search

Home  >  Q&A  >  body text

python - pandas groups by column A and column B, and averages column C. How to generate a dataframe of columns A, B, and C?

df = df.groupby(['A','B'])['c'].mean()

In this way, df is grouped, but how to turn df into a dataframe with columns A, B, and C?
Thanks

怪我咯怪我咯2741 days ago791

reply all(2)I'll reply

  • 为情所困

    为情所困2017-05-24 11:37:08

    I asked this question on stackoverflow, and some netizens gave me the answer. In fact, you only need to add one parameter. The method is as follows:

    df = df.groupby(['A','B'],as_index = False)['C'].mean()

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-24 11:37:08

    At least one reassignment is needed to extract these three columns. The grouping method seems to be like yours

    reply
    0
  • Cancelreply