How to use tuple as index in Pandas?

I have a dataframe with columns A, B, F and C. What I want to do is reassign values based on a tuple of columns, like this:

df[('A','B')=(0,3)] = 5

Which means, where A=0 and B=3, the values at the last column (C) should be 5.

What’s the best way to do this?

You cannot index by columns directly, you should either make A/B the index, or use boolean indexing:

df.loc[df['A'].eq(0) & df['B'].eq(3), 'C'] = 5

Or, if you have multiple conditions in parallel, use a merge:

out = (df[['A', 'B']]
 .merge(pd.DataFrame([{'A': 0, 'B': 3, 'C': 5}]), how='left')
 .combine_first(df)
 .astype(df.dtypes)
)

Output:

   A  B  C  F
0  0  1  1  1
1  0  3  5  2
2  1  1  3  3
3  1  3  4  4

Used input:

   A  B  F  C
0  0  1  1  1
1  0  3  2  2
2  1  1  3  3
3  1  3  4  4

Leave a Comment