pandas 如何使用pandas删除数据框中具有空列的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51374068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:48:41  来源:igfitidea点击:

how to remove a row which has empty column in a dataframe using pandas

pythonpandas

提问by tiru

I have to remove entire row with the column, which has no value my dataframe looks like

我必须用列删除整行,我的数据框看起来没有价值

Name   place    phonenum

mike   china     12344
       ireland    897654
suzzi  japan      09876
chang  china      897654
       Australia  897654
       india      876543

required output should be

所需的输出应该是

Name   place    phonenum

mike   china     12344
suzzi  japan      09876
chang  china      897654

I have used df1=df[df.Name == '']I got output

我用过df1=df[df.Name == '']我有输出

  Name   place    phonenum

Please help me

请帮我

回答by jezrael

If Nameis column:

如果Name是列:

print (df.columns)
Index(['Name', 'place', 'phonenum'], dtype='object')

Need change ==to !=for not equal if missing values are empty strings:

需要改变==!=不等于如果缺失值是空字符串:

print (df)
    Name      place  phonenum
0   mike      china     12344
1           ireland    897654
2  suzzi      japan      9876
3  chang      china    897654
4         Australia    897654
5             india    876543

df1 = df[df.Name != '']
print (df1)
    Name  place  phonenum
0   mike  china     12344
2  suzzi  japan      9876
3  chang  china    897654

If in first columns are NaNs use dropnawith specify column for check:

如果在第一列中NaN使用dropna指定列进行检查:

print (df)
    Name      place  phonenum
0   mike      china     12344
1    NaN    ireland    897654
2  suzzi      japan      9876
3  chang      china    897654
4    NaN  Australia    897654
5    NaN      india    876543

df1 = df.dropna(subset=['Name'])
print (df1)
    Name  place  phonenum
0   mike  china     12344
2  suzzi  japan      9876
3  chang  china    897654

回答by u9862891

DataFrame dropna() method will drop entire row if any value in the row is missing.

如果行中的任何值丢失,DataFrame dropna() 方法将删除整行。

df1 = df.dropna()

回答by StudentAtLU

In my case, I had a bunch of fields with dates, strings, and one column for values (also called "Value"). I tried all suggestions above, but what actually worked was to drop NA records for the "Value" field.

就我而言,我有一堆带有日期、字符串和一列值(也称为“值”)的字段。我尝试了上面的所有建议,但真正有效的是删除“值”字段的 NA 记录。

df = df.dropna(subset=['Value'])

df = df.dropna(subset=['Value'])