user5813071
user5813071

Reputation:

Pandas: Replace column if 0 if it is not the max

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

Answers (1)

Quang Hoang
Quang Hoang

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

Related Questions