简体   繁体   中英

Combining data from different rows based on the cell content and creating new columns based on the cell values with pandas and python

I have data in csv file where in every row there's a name, a fruit and amount related to the fruit. What i want is to combine the data from different rows to a single row where all amounts for fruits related to a certain name is under one row.

I have trouble finding a proper way of reading all the data from the fruit column and converting those fruit values to individual rows.

Also the null values has to be converted to zero (but that might be quite easy to do).

I'm using python and pandas dataframe, but i'm quite new to coding and pandas so i'm not that familiar doing this.

So this an example of the data I have.

name, fruit,     amount
Mike, Banana,    2
Mike, Kiwi,      3
Anna, Apple,     10
Anna, Banana,    20
Anna, Pineapple, 40
Bert, Pineapple, 100

And this is the format i want it to be:

name, Banana, Kiwi, Apple, Pineapple
Mike, 2,      3,    0,     0
Anna, 20,     0,    10,    40
Bert, 0,      0,    0,     100

Try to use pivot table when you want to reshape a dataframe.

df.pivot(index='name', columns='fruit', values='amount')

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