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.
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"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With