-1

I'm trying to create a script for analysing my datas. Here is my problem. Let's say I have an excel file like that.

Code Value
A1    20
B1    30
A1    15
C1    20
B1    20

I need Pandas to do this file like this. And write an excel file. I got an excel file like this.

A1 35
B1 50
C1 20

Code pretty much like this.

import pandas as pd

data = pd.read_excel("filename.xlsx")

Missing Part Here

x.to_excel(r'filename.xlsx', index = True, header=True)

And I need missing part. Thanks a lot for your solutions already.

Trying to automate my data analysis. Expect adding part of my script.

ddejohn
  • 8,775
  • 3
  • 17
  • 30
Safak
  • 1

1 Answers1

1

What you are looking for is groupby:

>>> df
  Code  Value
0   A1     20
1   B1     30
2   A1     15
3   C1     20
4   B1     20

>>> df.groupby('Code', as_index=False)['Value'].sum()
  Code  Value
0   A1     35
1   B1     50
2   C1     20

Please take the time to read 10 minutes to Pandas

Corralien
  • 109,409
  • 8
  • 28
  • 52