Reputation: 596
Let's say I have a dataframe:
0 1 2 3 4 5
0 1A 2 3 4 nan 6
1 1 2 3 nan 5 nan
2 1A 2 nan nan nan nan
3 1 2 nan nan nan nan
4 1 2 3 4 5 6
I want to delete rows with a condition in df[0] and where columns 2: are nan. I can't get the 2nd part.
I try to select the rows to drop using:
df.loc[:,2:].isnull()
but that selects rows with any nulls in the last columns, I want the rows where all the columns from 2: are null
I don't want to name the columns because the dataframe won't always have the same number of columns. The only solution I'm thinking of now would be to determine the number of columns in the dataframe, then use
df.loc[x,2].isnull() & df.loc[x,3].isnull() ...
which seems clunky. Ideally I want a way to check if there are nulls in all columns after column 2 (df.loc[x,2:])
Upvotes: 1
Views: 4657
Reputation: 596
Quang Hong gave me an answer in a comment that is what I was looking for:
df.loc[:,2:].isnull().all(1)
Upvotes: 3