0

I am using pandas for the first time.

df.groupby(np.arange(len(df))//10).mean()

I used the code above which works to take an average of every 10th row. I want to save this updated data frame but doing df.to_csv is saving the original dataframe which I imported.

I also want to multiply one column from my df (df.groupby dataframe essentially) with a number and make a new column. How do I do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
Hassan
  • 15
  • 5

1 Answers1

0

The operation: df.groupby(np.arange(len(df))//10).mean()

Might return the averages dataframe as you want it, but it wont change the original dataframe. Instead you'll need to do:

df_new = df.groupby(np.arange(len(df))//10).mean()

You could assign it the same name if you want. The other options is some operations which you might expect to modify the dataframe accept in inplace argument which normally defaults to False. See this question on SO.

To create a new column which is an existing column multpied by a number you'd do:

df_new['new_col'] = df_new['existing_col']*a_number

Dharman
  • 30,962
  • 25
  • 85
  • 135
kabdulla
  • 5,199
  • 3
  • 17
  • 30