14
import pandas as pd
import numpy as np
datain = np.loadtxt(datafile)
df = pd.DataFrame(data = datain, columns = ["t","p","x","y","z"])
avg = df.groupby(["t"], sort=False)["p"].mean().rename(columns={1:"mean"})

This doesn't work, it tells me TypeError: rename() got an unexpected keyword argument "columns". It also doesn't work if I do this,

avg.rename(columns = {1:"mean"}, inplace=True)

I cannot figure out why, all documentation tells me that my columns call is correct. I just want to rename the blank column created by my "mean" call to have a string index. Anyone know why or how to fix this? All examples I've seen follow this format. Thanks.

Will
  • 677
  • 3
  • 11
  • 21
  • Have you tried reading the file in directly with pandas...pd.read_csv(datafile, delimiter = '\t') or similar? – mauve Feb 27 '19 at 19:26

4 Answers4

14

IIUC you could do this

import pandas as pd
df = pd.DataFrame({"a":np.arange(10),
                   "b":np.random.choice(["A","B"],10)})

avg = df.groupby("b", sort=False)["a"].mean()\
        .reset_index(name="mean")

or

avg = df.groupby("b", sort=False)["a"].mean().reset_index()\
        .rename(columns={"a":"mean"})

or

avg = df.groupby("b", sort=False, as_index=False)["a"].mean()\
        .reset_index()\
        .rename(columns={"a":"mean"})
rpanai
  • 12,515
  • 2
  • 42
  • 64
  • 1
    This worked like a charm, the middle method seemed the cleanest and most straightforward to read to me. Thanks. – Will Feb 27 '19 at 22:12
  • 1
    It's my personal favorite too. But I wanted to write down few options. – rpanai Feb 27 '19 at 22:18
9

I ran into this same problem and was also confused about what the issue was. When you call:

df.groupby(...)["p"]....rename(columns={1:"mean"})

the rename() is called on DataFrame["p"] which returns a Series object, not a DataFrame object. The rename() function for a Series object has no column parameter (because there's only 1 "column"). Sometimes, pandas will implicitly convert Series objects to DataFrames so its easy to miss. You could alternatively write

pd.Series.to_frame(df.groupby(...)["p"].mean().reset_index(), name='mean')

Anna
  • 91
  • 1
  • 1
2

I think this should work:

avg = df.groupby(["t"], sort=False)["p"].mean().rename('mean').reset_index()
Gonzalo Garcia
  • 6,192
  • 2
  • 29
  • 32
kina_re
  • 41
  • 1
  • This gives me TypeError: 'str' object is not callable ... I'm unsure why as I don't fully understand the way rename and reset_index work. – Will Feb 27 '19 at 22:11
0

I think the problem comes from the fact that when you called:

avg = df.groupby("b", sort=False)["a"].mean().reset_index().rename(columns={"a":"mean"})

This line:

avg = df.groupby("b", sort=False)["a"].mean().reset_index() 

returns a pd.Series, not a pd.DataFrame. Normally if you drop the parameters of the column it should work:

avg = df.groupby("b", sort=False)["a"].mean().reset_index().rename("mean")
RiveN
  • 2,595
  • 11
  • 13
  • 26