熊猫:如何删除熊猫数据帧中所有列的前导缺失值?

使用大熊猫数据帧的形式:


     A     B     C

ID                

1   10   NaN   NaN

2   20   NaN   NaN

3   28  10.0   NaN

4   32  18.0  10.0

5   34  22.0  16.0

6   34  24.0  20.0

7   34  26.0  21.0

8   34  26.0  22.0

如何删除不同数量的初始缺失值?最初,我想向前填充“新”列的最后一个值,所以我最终会得到这个:


    A     B     C

0  10  10.0  10.0

1  20  18.0  16.0

2  28  22.0  20.0

3  32  24.0  21.0

4  34  26.0  22.0

5  34  26.0  22.0

6  34  26.0  22.0

7  34  26.0  22.0

但我想在剩余的行上也有nans也是很自然的:


    A     B     C

0  10  10.0  10.0

1  20  18.0  16.0

2  28  22.0  20.0

3  32  24.0  21.0

4  34  26.0  22.0

5  34  26.0   NaN

6  34   NaN   NaN

7  34   NaN   NaN

以下是问题的直观表示形式:


以前:

http://img4.mukewang.com/63298c8d00017dc204310294.jpg

后:

http://img.mukewang.com/63298c960001583604260289.jpg

我提出了一个笨重的方法,使用for循环,我使用删除前导nan,计算我删除的值的数量(N),附加最后一个可用数字N次,并逐列构建新的数据帧。但事实证明,对于较大的数据帧来说,这是非常慢的。我觉得这已经是万能熊猫库的内置功能,但到目前为止我还没有找到任何东西。有没有人建议用一种不那么繁琐的方式来做到这一点?df.dropna()


使用示例数据集完成代码:


import pandas as pd

import numpy as np


# sample dataframe

df = pd.DataFrame({'ID':[1,2,3,4,5,6,7,8],

                    'A': [10,20,28,32,34,34,34,34],

                   'B': [np.nan, np.nan, 10,18,22,24,26,26],

                    'C': [np.nan, np.nan, np.nan,10,16,20,21,22]})

df=df.set_index('ID')


# container for dataframe

# to be built using a for loop

df_new=pd.DataFrame()


for col in df.columns:

    # drop missing values column by column

    ser = df[col]

    original_length = len(ser)

    ser_new = ser.dropna()


    # if leading values are removed for N rows.

    # append last value N times for the last rows

    if len(ser_new) <= original_length:

        N = original_length - len(ser_new)

        ser_append = [ser.iloc[-1]]*N

        #ser_append = [np.nan]*N

        ser_new = ser_new.append(pd.Series(ser_append), ignore_index=True)

    df_new[col]=ser_new


df_new



扬帆大鱼
浏览 88回答 2
2回答

Qyouu

这是一个纯粹的熊猫解决方案。使用应用根据前导 NaN 的数量向上移动值,并使用 ffill,df.apply(lambda x: x.shift(-x.isna().sum())).ffill()&nbsp; &nbsp; A&nbsp; &nbsp; &nbsp; B&nbsp; &nbsp; &nbsp; &nbsp;C1&nbsp; &nbsp;10&nbsp; 10.0&nbsp; &nbsp; 10.02&nbsp; &nbsp;20&nbsp; 18.0&nbsp; &nbsp; 16.03&nbsp; &nbsp;28&nbsp; 22.0&nbsp; &nbsp; 20.04&nbsp; &nbsp;32&nbsp; 24.0&nbsp; &nbsp; 21.05&nbsp; &nbsp;34&nbsp; 26.0&nbsp; &nbsp; 22.06&nbsp; &nbsp;34&nbsp; 26.0&nbsp; &nbsp; 22.07&nbsp; &nbsp;34&nbsp; 26.0&nbsp; &nbsp; 22.08&nbsp; &nbsp;34&nbsp; 26.0&nbsp; &nbsp; 22.0

阿晨1998

我们可以利用每个序列,并按缺失值的数量移动每个序列shiftd = df.isna().sum(axis=0).to_dict() # calculate the number of missing rows per column&nbsp;for k,v in d.items():&nbsp; &nbsp; df[k] = df[k].shift(-v).ffill()--print(df)&nbsp; &nbsp;ID&nbsp; &nbsp;A&nbsp; &nbsp; &nbsp;B&nbsp; &nbsp; &nbsp;C0&nbsp; &nbsp;1&nbsp; 10&nbsp; 10.0&nbsp; 10.01&nbsp; &nbsp;2&nbsp; 20&nbsp; 18.0&nbsp; 16.02&nbsp; &nbsp;3&nbsp; 28&nbsp; 22.0&nbsp; 20.03&nbsp; &nbsp;4&nbsp; 32&nbsp; 24.0&nbsp; 21.04&nbsp; &nbsp;5&nbsp; 34&nbsp; 26.0&nbsp; 22.05&nbsp; &nbsp;6&nbsp; 34&nbsp; 26.0&nbsp; 22.06&nbsp; &nbsp;7&nbsp; 34&nbsp; 26.0&nbsp; 22.07&nbsp; &nbsp;8&nbsp; 34&nbsp; 26.0&nbsp; 22.0
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python