本文介绍了在不删除整个ROW的情况下删除NaN'Cells'(Pandas,Python3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在我有一个这样的DF
Right now I have a DF like this
Word Word2 Word3
Hello NaN NaN
My My Name NaN
Yellow Yellow Bee Yellow Bee Hive
Golden Golden Gates NaN
Yellow NaN NaN
我希望从数据框中删除所有NaN单元.因此,最终看起来像这样,"Yellow Bee Hive"已移至第1行(类似于在excel中从列中删除单元格时发生的情况):
What I was hoping for was to remove all of the NaN cells from my data frame. So in the end, it would look like this, where 'Yellow Bee Hive' has moved to row 1 (similarly to what happens when you delete cells from a column in excel) :
Word Word2 Word3
1 Hello My Name Yellow Bee Hive
2 My Yellow Bee
3 Yellow Golden Gates
4 Golden
5 Yellow
不幸的是,这两个都不起作用,因为它们删除了整个行!
Unfortunately, neither of these work because they delete the Entire ROW!
df = df[pd.notnull(df['Word','Word2','Word3'])]
或
df = df.dropna()
有人有什么建议吗?我应该重新索引表格吗?
Anyone have any suggestions? Should I reindex the table?
推荐答案
import numpy as np
import pandas as pd
import functools
def drop_and_roll(col, na_position='last', fillvalue=np.nan):
result = np.full(len(col), fillvalue, dtype=col.dtype)
mask = col.notnull()
N = mask.sum()
if na_position == 'last':
result[:N] = col.loc[mask]
elif na_position == 'first':
result[-N:] = col.loc[mask]
else:
raise ValueError('na_position {!r} unrecognized'.format(na_position))
return result
df = pd.read_table('data', sep='\s{2,}')
print(df.apply(functools.partial(drop_and_roll, fillvalue='')))
收益
Word Word2 Word3
0 Hello My Name Yellow Bee Hive
1 My Yellow Bee
2 Yellow Golden Gates
3 Golden
4 Yellow
这篇关于在不删除整个ROW的情况下删除NaN'Cells'(Pandas,Python3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!