Issue
In a project I'm working into I'm forced to use Pandas version 1.1.5. I'm trying to do a group by operation in order to aggregate a variable using multiple functions:
import pandas as pd
import numpy as np
df = pd.DataFrame( {
"Name" : ["Alice", "Bob", "James", "Mallory", "Bob" , "Lucas", "Alice", "Bob", "James", "Mallory", "Bob" , "Lucas"] ,
"Apples" : [22, 31, 35, 41, 27, 32, 64, 12, 59, 45, 65, 31] } )
apple_df = df.groupby('Name', as_index = False).agg(
apple_avg = ('Apples', np.mean),
apple_median = ('Apples', np.median),
apple_count = ('Apples', np.count_nonzero)
)
apple_df
I'm expecting the Name column with the other aggregation variables as result like this:
But I'm getting the following:
Any known bug and workaround for this issue?
P.S. All works fine with Pandas 1.3.0, but I can't use it in this project.
Solution
You could try to remove the as_index parameter and make a .reset_index() instead:
apple_df = df.groupby('Name').agg(
apple_avg = ('Apples', np.mean),
apple_median = ('Apples', np.median),
apple_count = ('Apples', np.count_nonzero)
).reset_index()
Answered By - Andreas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.