Error When Calling A Groupby Object Inside A Pandas Dataframe
I've got this dataframe: person_code #CNAE growth size 0 231 32 0.54 32 1 233 43 0.12 333 2 432 32 0.44 2
Solution 1:
There is one way, convert a to dict , then map it back
#a=df.groupby('#CNAE',group_keys=False).apply(pd.DataFrame.nlargest,n=3,columns='growth')df['top3growth']=df['#CNAE'].map(a.groupby('#CNAE').apply(lambdax :x.to_dict()))dfOut[195]:person_code#CNAE growth size \0231320.54321233430.123332432320.44213431560.32234654890.12895764320.202116434320.8290top3growth0 {'person_code': {0:231, 2:432, 6:434}, 'gro...
1 {'person_code': {1:233}, 'growth': {1:0.12},...2 {'person_code': {0:231, 2:432, 6:434}, 'gro...
3 {'person_code': {3:431}, 'growth': {3:0.32},...4 {'person_code': {4:654}, 'growth': {4:0.12},...5 {'person_code': {0:231, 2:432, 6:434}, 'gro...
6 {'person_code': {0:231, 2:432, 6:434}, 'gro...
After create your new column , if you want to convert the single cell back to dataframe
pd.DataFrame(df.top3growth[0])
Out[197]:
#CNAE growth person_code size
0320.54231322320.44432216320.8243490
Post a Comment for "Error When Calling A Groupby Object Inside A Pandas Dataframe"