【发布时间】:2018-03-12 13:29:07
【问题描述】:
我有一个大型数据框,其值如下:
Name A B C D E F G # Coulmns
Matt 1 n n n 5 n 5 # rows
Jake n n 2 n 3 n n
Paul 2 n 3 n n 8 n
我只想用前面的值填充 NA 值: 这是通过
实现的df.fillna(method='ffill')
这是前向填充方法,它给我一个输出:
Name A B C D E F G # Coulmns
Matt 1 1 1 1 5 5 5 # rows
Jake n n 2 2 3 3 3
Paul 2 2 3 3 3 8 8
问题是我不希望它走到最后,如果后面没有更多的值就停下来。请参阅第 2 行,杰克它必须在 E 处停止,因为它在最后一列中没有任何值。 期望的输出:
Name A B C D E F G # Coulmns
Matt 1 1 1 1 5 5 5 # rows
Jake n n 2 2 3 n n
Paul 2 2 3 3 8 8 n
有没有办法用熊猫做这个?
代码:
for i in range(len(df)):
df.loc[i][1:].fillna(method='ffill',inplace=True)
【问题讨论】:
-
第一行
Matt是否正确? -
抱歉,已编辑
标签: python python-3.x pandas dataframe missing-data