简体   繁体   中英

How to sum values each row in csv which belongs to specific object?

I have this csv:

ProductID, Price
P1, 10
P1, 19
P2, 68
P3, 18
P1, 20

How to sum total price of each ProductID, the result like this:

ProductID, Total
P1, 49
P2, 58
P3, 18

Thanks for your help!

Read the data into pandas and then use a group_by sum combination.


import pandas as pd

pandas_data_frame = pd.read_csv('path_to_csv_file')

pandas_data_frame.groupby(['ProductID']).sum()

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