I have a MultiIndex DataFrame:
predicted_y actual_y predicted_full actual_full
subj_id org_clip
123 3 2 5 [1, 2, 3] [4, 5, 6]
That I wish to add a new row to:
predicted_y actual_y predicted_full actual_full
subj_id org_clip
123 3 2 5 [1, 2, 3] [4, 5, 6]
321 4 20 50 [10, 20, 30] [40, 50, 60] # add this row
And the following code does it:
df.loc[('321', 4),['predicted_y', 'actual_y']] = [20, 50]
df.loc[('321', 4),['predicted_full', 'actual_full']] = [[10,20,30], [40,50,60]]
But when trying to add a new row in a single line, I'm getting an error:
df.loc[('321', 4),['predicted_y', 'actual_y', 'predicted_full', 'actual_full']] = [20, 50, [10,20,30], [40,50,60]]
>>> ValueError: setting an array element with a sequence.
Notes:
I believe it has something (possibly syntactic) to do with me trying to add a row that contains both values and lists. All other attempts had raised the same error; see the following examples:
df.loc[('321', 4),['predicted_y', 'actual_y', ['predicted_full', 'actual_full']]] = [20, 50, [10,20,30], [40,50,60]]
df.loc[('321', 4),['predicted_y', 'actual_y', ['predicted_full'], ['actual_full']]] = [20, 50, [10,20,30], [40,50,60]]
df.loc[('321', 4),['predicted_y', 'actual_y', [['predicted_full'], ['actual_full']]]] = [20, 50, [10,20,30], [40,50,60]]
df.loc[('321', 4),['predicted_y', 'actual_y', 'predicted_full', 'actual_full']] = [20, 50, np.array([10,20,30]), np.array([40,50,60])]
The code to construct the initial DataFrame:
df = pd.DataFrame(index=pd.MultiIndex(levels=[[], []], labels=[[], []], names=['subj_id', 'org_clip']),
columns=['predicted_y', 'actual_y', 'predicted_full', 'actual_full'])
df.loc[('123', 3),['predicted_y', 'actual_y']] = [2, 5]
df.loc[('123', 3),['predicted_full', 'actual_full']] = [[1,2,3], [4,5,6]]
