I have the following multiIndex dataframe:
df=
id/uniqueID var1 var2 var3
5171/0 10.0 2.8 0.0
5171/1 40.9 2.5 3.4
5171/2 60.7 3.1 5.2
...
5171/57 0.5 1.3 5.1
4567/0 1.5 2.0 1.0
4567/1 4.4 2.0 1.3
4567/2 6.3 3.0 1.5
...
4567/57 0.7 1.4 1.6
...
9584/0 0.3 2.6 0.0
9584/1 0.5 1.2 8.3
9584/2 0.7 3.0 5.6
...
9584/57 0.7 1.3 0.1
indexes_df=
id labeled_idxs
5171 [0,1,3,6,49,50]
4567 [45,46,47,56,57]
9584 [21]
...
I need to add a new binary column to df
with 1
or True
for the indexes labeled in the second dataframe, indexes_df
, like this:
df=
id/uniqueID var1 var2 var3 labels
5171/0 10.0 2.8 0.0 1
5171/1 40.9 2.5 3.4 1
5171/2 60.7 3.1 5.2 0
...
5171/57 0.5 1.3 5.1 0
4567/0 1.5 2.0 1.0 0
4567/1 4.4 2.0 1.3 0
4567/2 6.3 3.0 1.5 0
...
4567/56 0.4 0.4 1.3 1
4567/57 0.7 1.4 1.6 1
...
9584/0 0.3 2.6 0.0 0
9584/1 0.5 1.2 8.3 0
9584/2 0.7 3.0 5.6 0
...
9584/21 2.7 0.0 0.6 1
...
9584/57 0.7 1.3 0.1 0
I tried to do it with the following code and similar approaches but they all failed with SyntaxError
:
df['labes'] = indexes_df['labeled_idxs'].apply(lambda x: [i>0 ? 1 : 0 for i in x]))
How can I get the results I need?