簡體   English   中英

用Pandas中的先前值填充多列內容的nan行

[英]Fill nan rows of multiple columns' contents with previous values in Pandas

對於一個excel文件,用pandas閱讀后,我得到一個數據框,如下所示:

   type sub_type  num
0     a       a1    1
1   NaN      NaN    2
2   NaN       a2    3
3     b       b1    4
4   NaN      NaN    5
5   NaN       b2    6
6   NaN      NaN    7
7     c       c1    8
8   NaN      NaN    9
9   NaN      NaN   10
10  NaN       c2   11

我如何獲得這樣的預期結果? 謝謝。

   type sub_type  num
0     a       a1    1
1     a       a2    2
2     a       a2    3
3     b       b1    4
4     b       b2    5
5     b       b2    6
6     b       b3    7
7     c       c1    8
8     c       c2    9
9     c       c3   10
10    c       c2   11

您可以在固定列中使用fillna中的正向填充:

df['ColumnNameORIndex'] = df['ColumnNameORIndex'].fillna(method='ffill')

或完整的dataFrame中:

df = df.fillna(method='ffill')

是的,我們可以做到,但是沒有那么容易

df.type=df.type.ffill()# first ffill with type 
s=df.groupby([df.type,df.sub_type.notnull().cumsum()]).cumcount().add(df.sub_type.str[1:].astype(float).ffill(),fill_value=0).astype(int).astype(str).radd(df.type)
# then we create the sub group with the notnull value to find the sub id
# and get the number of values within each subgroup add the first value sub_id
df.sub_type.fillna(s,inplace=True)
df
   type sub_type  num
0     a       a1    1
1     a       a2    2
2     a       a2    3
3     b       b1    4
4     b       b2    5
5     b       b2    6
6     b       b3    7
7     c       c1    8
8     c       c2    9
9     c       c3   10
10    c       c2   11

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM