0

I'm studying python the last 3 weeks, and now I'm trying to calculate the average of a column "user", until the other column "date" has the same value, when "date" changes then it should calculate the average for the next date. My csv file looks like this:

[CSV File][1]

It should give me the average of all values in "user" wherever its the same date. When the date changes then it should give me the average for the next date and so on.

Davidoff
  • 21
  • 5
  • 2
    Look into `pandas groupby` function. – Mayank Porwal Sep 29 '20 at 07:30
  • [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Danila Ganchar Sep 29 '20 at 07:32
  • @MayankPorwal I have used pandas groupby function like this: data = pd.read_csv("my.csv").groupby('date').mean() and I've gotten an KeyError:'date'. Why does this happens? – Davidoff Sep 29 '20 at 07:44
  • The error basically says, that there is no column by the name `date` in your data frame. Do this: `df = pd.read_csv("my.csv")`, then check the output of `df.columns.tolist()`. – Mayank Porwal Sep 29 '20 at 07:51
  • @MayankPorwal this is my output when I do print(data.columns.tolist()) -> ['user;date'] I have the column named date. – Davidoff Sep 29 '20 at 08:12
  • it look like it makes my two columns into one, and sees user and date as one coulumn. – Davidoff Sep 29 '20 at 08:19
  • Yes, both your columns are combined into one single column. Do this: `df = pd.read_csv("my.csv", sep=';')` – Mayank Porwal Sep 29 '20 at 08:26

0 Answers0