简体   繁体   中英

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

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

  Name   place    phonenum

Please help me

If Name is column:

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 NaN s use dropna with specify column for check:

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

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.

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

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

df1 = df.dropna()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM