r/pythonhelp • • Jul 20 '23

Group data in CSV file

I have a CSV file with the following

+ABCDEFG
1Store NumberDevice NameSerial NumberMedia NameLife Counter_1Life Counter_2Life Counter Difference
213DRS80 Dxxxxx8x103089731460563
313SSDS6801xxxxx4x6(a5)2369952402003205
413SDS6801xxxxx5x77883379310477
513SDS4801xxxxx4x87132571683358
614SDS6223xxxxx5x76763684784
714SDS6223xxxxx6x868448705132065
814SDS805xxxxx8x103701337242229
914SDS6223xxxxx6x854892561551263
1014SDS6223xxxxx6x861674636551981
1114SDS6223xxxxx6x866583681551572
1214SDS6223xxxxx5x766176741124
1323SDS6801xxxxx5x7241573241681108
1423SDS6801xxxxx5x7418004189191
1523SDS6801xxxxx4x6(a5)2327552341021347
1623SDS4801xxxxx4x8293602945595
1723DRS80 Dxxxxx8x107501375168155
1823SDS6801xxxxx4x6(a5)172003172745742
1930SDS6223xxxxx6x830519318001281
2030SDS805xxxxx8x102484024982142
2130SDS6223xxxxx6x825440269201480
2230SDS6223xxxxx6x835097367151618
2330SDS6223xxxxx5x72190822208300

What I would like to do is group the data by store number AND media name and then add up all the values for each combined media type using the initial values in the Life Counter Difference column

The desired output would be similar to below

+ABC
1Store NumberMedia NameTotals
2138x10563
3134x6(a5)3205
4135x7477
5134x8358
6145x7208
7146x86881
8148x10229
9234x6(a5)2089
10235x7199
11238x10155
12234x895
13306x84379
14305x7300
15308x10142

Any idea how I could go about this?

2 Upvotes

2 comments sorted by

•

u/AutoModerator Jul 20 '23

Note:

  • This sub went private for a few days recently in solidarity with other subs who are hoping to get Reddit to reconsider some changes that they have proposed. These changes will affect the Reddit API and many third-party apps that access Reddit. If you are not already aware of the proposed changes, please read up on the topic and the ongoing protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

3

u/joseb Jul 21 '23

Ahh I think I figured it out.

I ran the following

df1 = df1.groupby(["Store Number", 'Media Name'])['Life Counter Difference'].sum()

And it output the desired result. I think it's OK but any thoughts on different methods would be appreciated.