简体   繁体   中英

how do i group the values of csv file using panda based on values of row?

I have a csv file which has data in this format. Here i want to group by elements using 2 column values.

col1  col2  col3
abc   001    mango
abc   001    apple
abc   001    orange
abc   002    potato
xyz   003    cabbage
xyz   003    peas
xyz   004    ladyfinger

I want the output in this format

col1  col2  col3
abc   001   [mango,apple,orange]
abc   002   [potato]
xyz   003   [cabbage,peas]
xyz   004   [ladyfinger]

I want to group the values of col3 on basis of clo2 values.

try it:

df.groupby(['col1','col2']).agg({'col3': lambda col: list(col)}).reset_index()

or

df.groupby('col2').agg(col1=pandas.NamedAgg(column='col1', aggfunc='first'), col3=pandas.NamedAgg(column='col3', aggfunc=lambda col: list(col))).reset_index()
df.groupby(['col1','col2']).agg({'col3': lambda col: list(col)}).reset_index()

You can achieve it with the code above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM