简体   繁体   中英

pandas groupby function is returning only null values as output

maindata['avg_delay']= maindata.groupby('name_customer')['Delay'].mean(numeric_only=False) 
maindata.avg_delay 

output:
0       NaT
1       NaT
2       NaT
4       NaT
5       NaT
         ..
49994   NaT
49996   NaT
49997   NaT
49998   NaT
49999   NaT
Name: avg_delay, Length: 40000, dtype: timedelta64[ns]

maindata.groupby('name_customer')['Delay'].mean(numeric_only=False) gives you a pd.Series with values of 'name_customer' as the Series's indices. Note that when you assign a pd.Series to a column of a dataframe, the assignment is index-by-index. Because your maindata 's indices is not values from 'name_customer' , the two sets of indices do not match with each other, and thus the result you observed.

As the doc says, transform :

Call function producing a like-indexed DataFrame on each group and return a DataFrame having the same indexes as the original object filled with the transformed values

so you may use the following line instead, but do check if the outcome is what you need or not.

maindata.groupby('name_customer')['Delay'].transform('mean')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM