Michael
Michael

Reputation: 559

Select the row which are NaN dataframe pandas

    0           1       
2   Ticket      Open Time   
3   5302684589  2020.06.17 01:32:13
...
4   5302717457  2020.06.17 03:11:57
5   NaN         0
6   Closed P/L: Closed P/L:
7   Ticket      Open Time
8   5302718245  2020.06.17 03:12:14
...

I have a database which I have loaded as dataframe by using pandas, I only need the first part data, namely for this example the rows from no.2 to no.4: we can use df.iloc[2:4] which is easy; \n

However, I have many dataframe that the first part data is not from #2 to #4. \n

Lucikly, for all dataframe there is one line which is NaN in the first column, for this example, no.5 is NaN in the begining, so I want to identify which row is NaN and then I can know the index of the NaN rows that I can quickly select the first part data I want. \n

my question is how can I select the data I want by using the NaN row, others may also have this problem, so I post it here

Upvotes: 0

Views: 78

Answers (3)

Michael
Michael

Reputation: 559

df[df.iloc[:,0].isnull()].index[0] can solve this problem, based on XXavier's answer, all credit comes to him, many thanks

Upvotes: 0

dataguy
dataguy

Reputation: 58

If you want a dataframe of non-nan rows, you can do a df.dropna(). Here is the official pandas documentation in case you want to drop inplace or column wise. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html

Upvotes: 0

XXavier
XXavier

Reputation: 1226

This should give you the first index of the row where all entries are nan

df[df.isnull().all(axis=1)].index[0]

You can now do the df.loc[0:df[df.isnull().all(axis=1)].index[0],:]

Upvotes: 1

Related Questions