Last updated: Apr 12, 2024
Reading time·5 min
The "pandas.errors.SpecificationError: nested renamer is not supported" error
occurs when you use the older, outdated syntax when calling the
DataFrame.agg()
method.
To solve the error, call the agg()
method with keyword arguments that map
names and functions.
Here is an example of how the error occurs.
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], }) # ⛔️ pandas.errors.SpecificationError: nested renamer is not supported series = df['salary'].agg({'salary': ['min', 'max']})
Passing a dictionary with a nested renamer is not supported in recent Pandas versions.
However, you could pass keyword arguments to the DataFrame.agg method to achieve the same result.
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], }) series = df['salary'].agg(min='min', max='max') # min 175.1 # max 205.4 # Name: salary, dtype: float64 print(series) print(series[0]) # 👉️ 175.1 print(series[1]) # 👉️ 205.4
We passed 2 keyword arguments that represent mappings from names to functions to
the agg()
method.
If you need to convert the result to a DataFrame
, use the pandas.DataFrame()
constructor.
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], }) df2 = pd.DataFrame(df['salary'].agg(min='min', max='max')) # salary # min 175.1 # max 205.4 print(df2)
agg()
on a GroupBy
objectThe same syntax can be used when you need to call the agg()
method on a
GroupBy
object.
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) # min max # name # Alice 175.1 205.1 # Bobby 180.2 350.2 # Carl 190.3 500.1 print(df.groupby(['name'])['salary'].agg(min='min', max='max'))
When calling the agg()
method, make sure the specified columns exist.
The first call to agg()
in the example below returns a DataFrame
successfully.
However, the second call raises an error because the experience
column doesn't
exist in the DataFrame
.
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], }) # salary 205.4 # dtype: float64 print(df.agg({'salary': 'max'})) # ⛔️ Error. The `experience` column does not exist print(df.agg({'salary': 'max', 'experience': 'min'}))
Make sure to only pass existing column keys to the agg()
method.
agg()
methodYou can also pass a list of functions to the agg()
method.
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) print(df.groupby(['name'])['salary'].agg(['max', 'min', 'mean']))
Running the code sample produces the following output.
max min mean name Alice 205.1 175.1 190.1 Bobby 350.2 180.2 265.2 Carl 500.1 190.3 345.2
If you need to rename the columns after calling agg()
, use the
columns attribute.
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) df2 = df.groupby(['name'])['salary'].agg(['max', 'min', 'mean']) print(df2) print('-' * 50) df2.columns = ['max_salary', 'min_salary', 'mean_salary'] print(df2)
Running the code sample produces the following output.
max min mean name Alice 205.1 175.1 190.1 Bobby 350.2 180.2 265.2 Carl 500.1 190.3 345.2 -------------------------------------------------- max_salary min_salary mean_salary name Alice 205.1 175.1 190.1 Bobby 350.2 180.2 265.2 Carl 500.1 190.3 345.2
Passing a list of functions to the agg()
method is allowed, however, passing a
nested dictionary is not.
Therefore, we can just call the method with a list of functions and rename the columns if necessary.
agg()
with duplicated function names without assigning a column nameThe error also occurs when you call the agg()
method with duplicated function
names without assigning a column name.
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) # ⛔️ pandas.errors.SpecificationError: Function names must be unique if there is no new column names assigned df2 = df.groupby(['name']).agg(['max', 'min', 'min']) print(df2)
Notice that we passed the min
function twice to agg()
.
Make sure you haven't forgotten to select a specific column.
For example, the following is valid.
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) df2 = df.groupby(['name'])['salary'].agg(['max', 'min', 'min']) # max min min # name # Alice 205.1 175.1 175.1 # Bobby 350.2 180.2 180.2 # Carl 500.1 190.3 190.3 print(df2)
The following is also valid because we no longer have duplicate function names
in the call to agg()
.
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) df2 = df.groupby(['name']).agg(['max', 'min']) # salary # max min # name # Alice 205.1 175.1 # Bobby 350.2 180.2 # Carl 500.1 190.3 print(df2)
You can learn more about the related topics by checking out the following tutorials: