Reputation:
What I want to do is replace a column value with "0" if, for a given row, that column is not maximal. And conversely, replace the column value with "1" if, for a given row, that column IS maximal.
My data looks like this:
data = {
"A": [1, 2, 3],
"B": [3, 5, 1],
"Max": ["B", "B", "A"]
}
data_df = pd.DataFrame(data)
print(data_df)
A B Max
0 1 3 B
1 2 5 B
2 3 1 A
But I want it to look like
A B Max
0 0 1 B
1 0 1 B
2 1 0 A
Upvotes: 3
Views: 157
Reputation: 150775
You can do:
for col in ['A','B']:
data_df[col] = data_df['Max'].eq(col).astype(int)
Or, you could do:
data_df[['A', 'B']] = (pd.get_dummies(data_df['Max'])
.reindex(['A','B'], axis=1, fill_value=0)
)
Upvotes: 2