I am trying to update a dataframe row based on a list of tuples. The tuple will contain a colum name (stage name) and value for stage depending on if it passed or not.
example:
Stage 1 SUCCESS
stage 2 SUCCESS
stage 3 SUCCESS
stage 4 DELAYED
stage 5 PENDING
I generated a empty data frame and populated the colum names and the stage names like so ...
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
However, I can't seem to update the colum correctly by extracting the tuple and using a condition to update the row. No matter what I try...
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)
the result is alway the same:
project_name Stage 1 Stage 2 Stage 3 Stage 4 Stage 5
0 [project 1] NaN NaN NaN NaN NaN
Can someone tell me what I am missing?
Thanks,
E
CodePudding user response:
I think here is problem is list
in column project_name
, so if compare get False and no update. Solution is select first value by indexing:
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