18
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.

1
  • Have you tried reading the file in directly with pandas...pd.read_csv(datafile, delimiter = '\t') or similar? Commented Feb 27, 2019 at 19:26

4 Answers 4

16

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"})
Sign up to request clarification or add additional context in comments.

2 Comments

This worked like a charm, the middle method seemed the cleanest and most straightforward to read to me. Thanks.
It's my personal favorite too. But I wanted to write down few options.
11

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')

Comments

2

I think this should work:

avg = df.groupby(["t"], sort=False)["p"].mean().rename('mean').reset_index()

1 Comment

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.
2

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.