Home > Back-end >  Find the first numeric value in a column in Pandas using python
Find the first numeric value in a column in Pandas using python

Time:01-07

I have a dataframe that consists of a header of a variable number of lines, then it has several columns of numeric data

The dataframe looks something like this (I've skipped a bunch of rows, but it's basically the same):

Index Unnamed: 0 Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4 Unnamed: 5
0 NaN Job No: 21-05-23384 NaN NaN NaN
1 NaN Client: Bob Canada NaN NaN NaN
2 NaN Project: Uncle 3 NaN NaN NaN
3 NaN Sounding ID: SCPT21 NaN NaN NaN
4 NaN Date: 04-Dec-2021 NaN NaN NaN
14 Depth (m) Header Header Header Header
15 0.92 0.90 NaN NaN NaN
16 1.92 1.90 245 1.34 55

All I want to get is the index of the row the first value is in, 15. I'm not sure how to do it. Thanks.

CodePudding user response:

In my mind, the most logical way is to treat the Unnamed: 0 column rows as strings, return the first character of each string and determine if the character is numeric or not with isnumeric(). This will return either True or False in a new column.

df['numeric'] = df['Unnamed: 0'].astype(str).str[0].str.isnumeric()

Next, I would take all the indexes where the numeric column is True, assign the index values to a list and take the first value from that list:

idx = df.index[df['numeric']].tolist()[0]
  •  Tags:  
  • Related