1

How do I prevent the .ffill() method to drop my grouping variable without overwriting my val column?

That is, not doing: samp_df['val'] = samp_df.groupby('ID').ffill()

Further, why does .groupby behaves this way and not the same way it does with .mean(), moving my grouping variable to the index?

import pandas as pd
import numpy as np

samp_df = pd.DataFrame({'ID': ['A', 'A', 'A', 'B', 'B', 'B'],
                        'val': [1, 2, np.nan, np.nan, 4, np.nan]})

print(samp_df.groupby('ID').mean())

    val
ID     
A   1.5
B   3.5

print(samp_df.groupby('ID').ffill())

   val
0  1.0
1  2.0
2  2.0
3  NaN
4  4.0
5  4.0
Rafael Neves
  • 467
  • 3
  • 10
  • 2
    mean is agg function ffill is not ~ to get the same output , do as_index=False within groupby – BENY Jan 15 '20 at 21:01
  • that does not work, if you do as_index=False it behaves the same way. – Rafael Neves Jan 15 '20 at 21:04
  • 1
    Does this answer your question? [pandas: Filling missing values within a group](https://stackoverflow.com/questions/18265930/pandas-filling-missing-values-within-a-group) – G. Anderson Jan 15 '20 at 21:12

0 Answers0