Replace values based on multiple conditions with groupby mean in Pandas
问题 Say I have a dataframe as follows: df = pd.DataFrame({'date': pd.date_range(start='2013-01-01', periods=6, freq='M'), 'value': [3, 3.5, -5, 2, 7, 6.8], 'type': ['a', 'a', 'a', 'b', 'b', 'b']}) df['pct'] = df.groupby(['type'])['value'].pct_change() Ouput: date value type pct 0 2013-01-31 3.0 a NaN 1 2013-02-28 3.5 a 0.166667 2 2013-03-31 -5.0 a -2.428571 3 2013-04-30 2.0 b NaN 4 2013-05-31 7.0 b 2.500000 5 2013-06-30 6.8 b -0.028571 I want to replace the pct values which is bigger than 0.2 or