pandas ffill based on condition in another column

后端 未结 1 745
我寻月下人不归
我寻月下人不归 2021-01-18 01:22

I have a pandas DataFrame as shown below.

df = pd.DataFrame({
    \'date\': [\'2011-01-01\', \'2011-01-01\', \'2011-02-01\', \'2011-02-01\', \'2011-03-01\',         


        
相关标签:
1条回答
  • 2021-01-18 01:55

    Use groupby:

    df.groupby('category').ffill()
    

    Output:

       category        date  rate
    0         1  2011-01-01  0.50
    1         2  2011-01-01  0.75
    2         1  2011-02-01  0.50
    3         2  2011-02-01  0.75
    4         1  2011-03-01  1.00
    5         2  2011-03-01  1.25
    6         1  2011-04-01  1.00
    7         2  2011-04-01  1.25
    

    If you have other columns with NaN that you don't want fill, then you can use this to just ffill NaN in rate column:

    df['rate'] = df.groupby('category')['rate'].ffill()
    
    0 讨论(0)
提交回复
热议问题