0

I want to calculate the mean of mulptiple rows that have one single value where they match and store it in another csv file. The given data is:

ID   salary days_of_work ...
1    2000   3            ...
1    1890   2            ...
1    2109   4            ...
2     .
2     .
2     .
2
3
3
...  

And then obtain in another file, for every ID, one single row that contains the mean of the datas of the other columns like this:

ID   salary     days_of_work ...
1    1999.6667  3 ...
2    ...
3    ...
.
.
.

Update:

I tried to do this but for a file that has utc_time instead of ID

import pandas as pd


keep_col = ['utc_time','temperature','pressure','humidity','wind_direction','wind_speed/kph']
pd.read_csv('Gridpoints.csv', names=keep_col).to_csv("GridPoints/test.csv", index=False)



f=pd.read_csv("Gridpoints"+".csv")
df = f[keep_col]

df.groupby(['utc_time']).mean()

df.to_csv("GridPoints/test.csv", index=False)

So first what I do is getting a column deleted and then on the dataframe obtained, I want to do it for the utc_time column but it doesn't do anything

LiquidSnake
  • 375
  • 1
  • 4
  • 16

1 Answers1

1

First you need to group by ID and then calculate the mean.

import pandas as pd

df = pd.read_csv('Book1.csv')

df1 = df.groupby(['ID'], as_index= False)[['Salary', 'days']].mean()
print(df1)

ID       Salary  days
1  1999.666667   3.0
user96564
  • 1,578
  • 5
  • 24
  • 42