I have one dataframe with several rows and several columns and I need to keep the values from the other columns a,b,c by combining it into a single row of the corresponding key.
Key, ColA, ColB, Colc
111 0 12 0
111 12 0 0
111 0 0 12
222 12 0 0
222 0 0 12
and the output I want is
key, ColA, ColB, ColC
111 12 12 12
222 12 0 12
Thanks for any help
CodePudding user response:
You can use groupby()
to get the max value :
import pandas as pd
df = pd.DataFrame({'Key' : [111, 111, 111, 222, 222], 'ColA' : [0, 12, 0, 12, 0], 'ColB' : [12, 0, 0, 0, 0], 'ColC' : [0, 0, 12, 0, 12]})
print(df.groupby(by='Key', as_index = False).max())
# Output :
# Key ColA ColB ColC
# 0 111 12 12 12
# 1 222 12 0 12
CodePudding user response:
Keep the max
of each group:
>>> df.groupby('Key', as_index=False).max()
Key ColA ColB Colc
0 111 12 12 12
1 222 12 0 12