Reputation: 23
I am writting this code but appear this error:
Code:
(df.set_index('STNAME')
.groupby(level=0)['CENSUS2010POP']
.agg({'avg': np.average, 'sum': np.sum})
)
Error:
D:\Programas core\Paython\ANACONDA\lib\site-packages\pandas\core\base.py in _aggregate(self, arg, *args, **kwargs)
364 obj.columns.intersection(keys)
365 ) != len(keys):
--> 366 raise SpecificationError("nested renamer is not supported")
367
368 from pandas.core.reshape.concat import concat
SpecificationError: nested renamer is not supported
Data: enter image description here
Upvotes: 2
Views: 9150
Reputation: 31226
head = "ROW SUMLEV REGION DIVISION STATE COUNTY STNAME CTYNAME CENSUS2010POP ESTIMATESBASE2010"
csv = '''1,50,3,6,1,1,Alabama,Autauga County,54571,54571
2,50,3,6,1,3,Alabama,Baldwin County,182265,182265
3,50,3,6,1,5,Alabama,Barbour County,27457,27457
4,50,3,6,1,7,Alabama,Bibb County,22915,22919
5,50,3,6,1,9,Alabama,Blount County,57322,57322'''
head.split(" ")
da = [[i for i in l.split(",")] for l in csv.split("\n")]
df = pd.DataFrame(da, columns=head.split(" "))
df = df.drop(columns=["ROW"])
df.CENSUS2010POP = pd.to_numeric(df.CENSUS2010POP)
df.groupby(["STNAME"])['CENSUS2010POP'].agg(["mean","sum"]).rename({"mean":"avg"}, axis=1)
df.groupby(["STNAME"])['CENSUS2010POP'].agg([np.average, np.sum]).rename({"average":"avg"}, axis=1)
df.set_index("STNAME").groupby(level=0)['CENSUS2010POP'].agg([np.average, np.sum]).rename({"average":"avg"}, axis=1)
Upvotes: 1
Reputation: 1883
The pandas named aggregate used here. Two options of codes below.
Option # 1:
Note: Removed {}
from .agg()
.
# Import libraries
import pandas as pd
# DataFrame
df = pd.DataFrame({
'STNAME': ['Alabama']*5,
'CENSUS2010POP': [54571,1822865,27457,22915,57322]
})
# Group and aggregate
x = (df.set_index('STNAME')
.groupby(level=0)['CENSUS2010POP']
.agg(avg= 'mean', sum= 'sum')
)
print(x)
Output
avg sum
STNAME
Alabama 397026 1985130
Option # 2:
Note: The output column name sum
was changed to total
to avoid conflict with np.sum
.
# Import libraries
import pandas as pd
# Create DataFrame
df = pd.DataFrame({
'STNAME': ['Alabama']*5,
'CENSUS2010POP': [54571,1822865,27457,22915,57322]
})
df = df.set_index('STNAME')
# Create grouped object
g = df.groupby(level=0)
# Named aggregated on grouped object
x = g.aggregate(
avg=pd.NamedAgg(column='CENSUS2010POP', aggfunc=np.mean),
total=pd.NamedAgg(column='CENSUS2010POP', aggfunc=np.sum),
)
# Output
print(x)
Output
avg total
STNAME
Alabama 397026 1985130
Upvotes: 6