【问题标题】:Pandas accessing last non-null value熊猫访问最后一个非空值
【发布时间】:2013-07-24 01:43:16
【问题描述】:

我想用给定组的最后一个有效值填充数据框 NaN。例如:

import pandas as pd
import random as randy
import numpy as np

df_size = int(1e1)                
df = pd.DataFrame({'category': randy.sample(np.repeat(['Strawberry','Apple',],df_size),df_size), 'values': randy.sample(np.repeat([np.NaN,0,1],df_size),df_size)}, index=randy.sample(np.arange(0,10),df_size)).sort_index(by=['category'], ascending=[True])

交付:

     category   value
7       Apple     NaN
6       Apple       1
4       Apple       0
5       Apple     NaN
1       Apple     NaN
0  Strawberry       1
8  Strawberry     NaN
2  Strawberry       0
3  Strawberry       0
9  Strawberry     NaN

我要计算的列如下所示:

     category   value  last_value
7       Apple     NaN         NaN
6       Apple       1         NaN
4       Apple       0           1
5       Apple     NaN           0
1       Apple     NaN           0
0  Strawberry       1         NaN
8  Strawberry     NaN           1
2  Strawberry       0           1
3  Strawberry       0           0
9  Strawberry     NaN           0

尝试了shift()iterrows(),但无济于事。

【问题讨论】:

    标签: python numpy pandas


    【解决方案1】:

    看起来你想先做一个ffill,然后再做一个shift

    In [11]: df['value'].ffill()
    Out[11]:
    7   NaN
    6     1
    4     0
    5     0
    1     0
    0     1
    8     1
    2     0
    3     0
    9     0
    Name: value, dtype: float64
    
    In [12]: df['value'].ffill().shift(1)
    Out[12]:
    7   NaN
    6   NaN
    4     1
    5     0
    1     0
    0     0
    8     1
    2     1
    3     0
    9     0
    Name: value, dtype: float64
    

    要对每个执行此操作,您必须先按类别分组,然后应用此功能:

    In [13]: g = df.groupby('category')
    
    In [14]: g['value'].apply(lambda x: x.ffill().shift(1))
    Out[14]:
    7   NaN
    6   NaN
    4     1
    5     0
    1     0
    0   NaN
    8     1
    2     1
    3     0
    9     0
    dtype: float64
    
    In [15]: df['last_value'] = g['value'].apply(lambda x: x.ffill().shift(1))
    

    【讨论】:

    • 我认为 OP 希望将这个技巧从 df.groupby("category") 的组中拉出来,这可以解释第三个 NaN。
    • @DSM :) 现在看起来很明显!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 2017-03-27
    • 2021-11-03
    • 1970-01-01
    • 2020-03-21
    • 2017-07-19
    相关资源
    最近更新 更多