11

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

1
  • What is print(df.index) ? Commented Jul 17, 2018 at 5:53

3 Answers 3

11

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 NaNs 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
Sign up to request clarification or add additional context in comments.

2 Comments

I used it but rows with empty column is not removed
Ok, what about df1 = df.dropna(subset=['Name']) ?
4

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'])

Comments

3

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

df1 = df.dropna()

1 Comment

by using this all columns which are empty entire row is deleting but i want to delete only Name column empty , then remove that itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.