Python renaming Pandas DataFrame Columns
Asked Answered
C

4

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

Cacuminal answered 27/2, 2019 at 18:56 Comment(1)
Have you tried reading the file in directly with pandas...pd.read_csv(datafile, delimiter = '\t') or similar?Unbeatable
S
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"})
Supertax answered 27/2, 2019 at 19:9 Comment(2)
This worked like a charm, the middle method seemed the cleanest and most straightforward to read to me. Thanks.Cacuminal
It's my personal favorite too. But I wanted to write down few options.Supertax
B
10

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

Bailsman answered 26/6, 2020 at 2:16 Comment(0)
I
2

I think this should work:

avg = df.groupby(["t"], sort=False)["p"].mean().rename('mean').reset_index()
Infancy answered 27/2, 2019 at 19:27 Comment(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.Cacuminal
K
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")
Keary answered 3/11, 2021 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.