Find the largest value by row and change the value

Asked 2 years ago, Updated 2 years ago, 36 views

As above, I want to change the highest value by row in the data frame to 1 and the remaining value to 0. What should I do?

python pandas

2022-09-20 21:48

1 Answers

https://stackoverflow.com/a/50536854/100093

m = np.zeros_like(df.values)
m[np.arange(len(df)), df.values.argmax(1)] = 1

df1 = pd.DataFrame(m, columns = df.columns).astype(int)


2022-09-20 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.