从 Pandas 的 DataFrame 中删除包含所有 NaN 的行 [英] Remove row with all NaN from DataFrame in pandas

查看:126
本文介绍了从 Pandas 的 DataFrame 中删除包含所有 NaN 的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据框 df1df2,我想将它们组合到新的数据框 df.然而,这会创建一个包含所有 NaN 的行:

<预><代码>>>>从熊猫导入数据帧>>>df1 = DataFrame({'col1':[1,2,3],'col2':[2,3,4]})>>>df2 = DataFrame({'col1':[4,2,5],'col2':[6,3,5]})>>>df = df2[~df2.isin(df1)]>>>df列 1 列 20 4 61 南南2 5 5

如何删除这一行,使 df 看起来像:

<预><代码>>>>df列 1 列 20 4 62 5 5

解决方案

你可以使用 dropna:

在 [13]: df2[df1 != df2].dropna(how='all')出[13]:列 1 列 20 4 62 5 5

I have two data frame df1, df2, which I want to combine to the new dataframe df. This however creates an row with all NaN:

>>> from pandas import  DataFrame
>>> df1 = DataFrame({'col1':[1,2,3], 'col2':[2,3,4]})
>>> df2 = DataFrame({'col1':[4,2,5], 'col2':[6,3,5]})

>>> df = df2[~df2.isin(df1)]
>>> df
   col1  col2
0     4     6
1   NaN   NaN
2     5     5

How can I remove this row, such that df looks like:

>>> df
   col1  col2
0     4     6
2     5     5

解决方案

You could use dropna:

In [13]: df2[df1 != df2].dropna(how='all')
Out[13]: 
   col1  col2
0     4     6
2     5     5

这篇关于从 Pandas 的 DataFrame 中删除包含所有 NaN 的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆