Si_CPyR
Si_CPyR

Reputation: 161

How to get index number for a row meeting specific condition

I am curious to know how to grab index number off of a dataframe that's meeting a specific condition. I've been playing with pandas.Index.get_loc, but no luck.

I've loaded a csv file, and it's structured in a way that has 1000+ rows with all column values filled in, but in the middle there is one completely empty row, and the data starts again. I wanted to get the index # of the row, so I can remove/delete all the subsequent rows that come after the empty row.

This is the way I identified the empty row, df[df["ColumnA"] ==None], but no luck in getting the row index number for that row. Please help!

Upvotes: 2

Views: 211

Answers (1)

jpp
jpp

Reputation: 164823

What you most likely want is pd.DataFrame.dropna:

Return object with labels on given axis omitted where alternately any or all of the data are missing

If the row is empty, you can simply do this:

df = df.dropna(how='all')

If you want to find indices of null rows, you can use pd.DataFrame.isnull:

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

To remove rows with indices greater than the first empty row:

df = df[df.index < res[0]]

Upvotes: 1

Related Questions