Reputation: 3375
I was doing a simple groupby, but got a deprecation / future warning message.
Here's my original groupby, which works 100% OK:
print(df)
customer_ID amount
0 joe 321
1 joe 5
2 joe 1
3 joe 3135
4 mary 35
5 mary 65
6 mary 68
7 mary 654
8 mary 1
9 john 5313
10 john 51
11 john 351
12 john 84
13 john 68
df.groupby('customer_ID')['amount'].agg({'count_transactions':'count','median_spend':'median'})
count_transactions median_spend
customer_ID
joe 4 163
john 5 84
mary 5 65
FutureWarning: using a dict on a Series for aggregation is deprecated and will be removed in a future version. Use named aggregation instead.
>>> grouper.agg(name_1=func_1, name_2=func_2)
So I decided to try their suggestion, which is grouper.agg(name_1=func_1, name_2=func_2)
But when I try the suggested method I get a SyntaxError
:
df.groupby('customer_ID')['amount'].agg('count_transactions'=count,'median_spend'=median)
SyntaxError: keyword can't be an expression
I've tried a few variations of this, such as adding / removing quotations, but can't figure out how to solve it.
I know my first groupby works fine.. but I'd still like to know where I'm going wrong with the suggestion from the future warning message.
Upvotes: 0
Views: 828
Reputation: 6056
Since it's a SyntaxError
, it can't be related with pandas
. You can get the same error with this:
>>> print("Hello", 'end'="")
File "<stdin>", line 1
SyntaxError: keyword can't be an expression
So the problem is with the quotes around keyword argument. You need to remove them.
You could also get rid of deprecation warning by unpacking your dict
with **
.
This
df.groupby('customer_ID')['amount'].agg(**{'count_transactions':'count','median_spend':'median'})
will work as same as this one:
df.groupby('customer_ID')['amount'].agg(count_transactions='count',median_spend='median')
Upvotes: 1
Reputation: 3375
Basically the functions have to be strings.
I had to add quotes around count
and median
functions, and remove quotes from new column names count_transactions
and median_spend
.
Solution:
df.groupby('customer_ID')['amount'].agg(count_transactions='count',median_spend='median')
Upvotes: 0