Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new column to pandas dataframe after groupby and rolling on a column

Tags:

pandas

I am trying to add a new column to pandas dataframe after groupby and rolling average but the newly generated column changes order after reset_index()

original dataframe

Name    Values
0   A   1
1   A   2
2   A   3
3   B   1
4   B   2
5   C   3
6   A   2
7   A   6
8   B   8
9   B   3
10  D   0

after groupby and rolling it looks something like:

Name    
A     0          NaN
      1          NaN
      2     2.000000
      6     2.333333
      7     3.666667
B     3          NaN
      4          NaN
      8     3.666667
      9     4.333333
C     5          NaN
D     10         NaN
Name: Values, dtype: float64

Now can someone help me to add this result in new column in the original dataframe? Because when I try to reset_index(), the order changes to the groupby order.

like image 957
bazinga Avatar asked Oct 15 '25 18:10

bazinga


2 Answers

Use apply to apply rolling mean on each group,

df['rolling_mean'] = df.groupby('Name').Values.apply(lambda x: x.rolling(3).mean())

    df  

  Name  Values  rolling_mean
0   A   1   NaN
1   A   2   NaN
2   A   3   2.000000
3   B   1   NaN
4   B   2   NaN
5   C   3   NaN
6   A   2   2.333333
7   A   6   3.666667
8   B   8   3.666667
9   B   3   4.333333
10  D   0   NaN
like image 152
Vaishali Avatar answered Oct 20 '25 11:10

Vaishali


Here is an example:

df = pd.DataFrame({'Name': {0: 'A',
  1: 'A',
  2: 'A',
  3: 'B',
  4: 'B',
  5: 'C',
  6: 'A',
  7: 'A',
  8: 'B',
  9: 'B',
  10: 'D'},
 'Values': {0: 1, 1: 2, 2: 3, 3: 1, 4: 2, 5: 3, 6: 2, 7: 6, 8: 8, 9: 3, 10: 0}})

df2 = pd.DataFrame({2: {('A', 0): np.nan,
  ('A', 1): np.nan,
  ('A', 2): 2.0,
  ('A', 6): 2.333333,
  ('A', 7): 3.666667,
  ('B', 3): np.nan,
  ('B', 4): np.nan,
  ('B', 8): 3.666667,
  ('B', 9): 4.3333330000000005,
  ('C', 5): np.nan,
  ('D', 10): np.nan}})

df.merge(df2.reset_index(level=0), left_index=True, right_index=True)


   Name  Values  0         2
0     A       1  A       NaN
1     A       2  A       NaN
2     A       3  A  2.000000
3     B       1  B       NaN
4     B       2  B       NaN
5     C       3  C       NaN
6     A       2  A  2.333333
7     A       6  A  3.666667
8     B       8  B  3.666667
9     B       3  B  4.333333
10    D       0  D       NaN

or join:

df.join(df2.reset_index(level=0))

   Name  Values  0         2
0     A       1  A       NaN
1     A       2  A       NaN
2     A       3  A  2.000000
3     B       1  B       NaN
4     B       2  B       NaN
5     C       3  C       NaN
6     A       2  A  2.333333
7     A       6  A  3.666667
8     B       8  B  3.666667
9     B       3  B  4.333333
10    D       0  D       NaN
like image 38
It_is_Chris Avatar answered Oct 20 '25 12:10

It_is_Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!