I have this df:
CODE MONTH PP
0 100007 01 22.1
1 100007 01 20
2 100007 01 5
3 100007 01 10
4 100007 01 12
... .. ..
10542747 155217 02 11
10542748 155217 02 12
10542749 155217 02 15
10542750 155217 02 18
10542751 155217 02 3
[10542752 rows x 3 columns]
I want to first group the df by df['CODE'] and df['MONTH']. And then convert the max value of the grouped df 'PP' column to nan.
So i did this code:
grouped_df=pd.DataFrame()
for i, data in df.groupby(['CODE','MONTH']):
data.loc[data['PP']==data['PP'].max(), 'PP']=np.nan
grouped_df=grouped_df.append(data)
But it takes too long to run. Like 15 minutes. Maybe cause i have [10542752 rows x 3 columns] in the df. But is there any way to improve this code to a faster one?
Thanks in advance