如何使用元组中的值更新数据框

How to update dataframe with values from a tuple

我正在尝试根据元组列表更新数据框行。该元组将包含列名称(阶段名称)和阶段值,具体取决于它是否通过。

示例:

Stage 1 SUCCESS
stage 2 SUCCESS
stage 3 SUCCESS
stage 4 DELAYED
stage 5 PENDING

我生成了一个空数据框并像这样填充了列名称和阶段名称...

df = df.append({'project_name' : current_project}, ignore_index=True)
       project_name    Stage 1    Stage 2    Stage 3    Stage 4   Stage 5 
0       [project 1]    NaN        NaN         NaN         NaN       NaN

但是,我似乎无法通过提取元组并使用条件更新行来正确更新列。不管我怎么努力...

for details in stage_data:
    (item, item_status) = details
    #print(item, item_status)
    df.loc[df.project_name == current_project, item] = item_status
print(df)   

结果总是一样的:

       project_name    Stage 1    Stage 2    Stage 3    Stage 4   Stage 5 
0       [project 1]    NaN        NaN         NaN         NaN       NaN

谁能告诉我我错过了什么?

谢谢,

E

我认为这里的问题是 project_name 列中的 list,所以如果比较得到 False 并且没有更新。解决方案是 select 索引的第一个值:

stage_data = [('Stage 1','SUCCESS'),('Stage 2','DELAYED')]
df = pd.DataFrame({'project_name': [['project 1'],['project 2']]})


current_project = 'project 1'
for details in stage_data:
    (item, item_status) = details
    #print(item, item_status)
    print (df.project_name == current_project)
    print (df.project_name.str[0] == current_project)
    
    0    False
    1    False
    Name: project_name, dtype: bool
    0     True
    1    False
    Name: project_name, dtype: bool


    0    False
    1    False
    Name: project_name, dtype: bool
    0     True
    1    False
    Name: project_name, dtype: bool

    df.loc[df.project_name.str[0] == current_project, item] = item_status
    
print (df)
  project_name  Stage 1  Stage 2
0  [project 1]  SUCCESS  DELAYED
1  [project 2]      NaN      NaN