rsc05
rsc05

Reputation: 3790

Pandas: Extract the index of the rows that seem to have a float variable

Suppose I have the following dataframe

import pandas as pd
df = pd.DataFrame( {3: {0: 'Total shareholders’ equity, beginning balances', 1: 'Total shareholders’ equity, beginning balances', 2: 'Total shareholders’ equity, beginning balances', 3: '$', 4: '90488', 9: '$', 10: '107147', 15: '$', 16: '134047'}} )

As you can see that index 4, 10, 16 are numbers. How can I extract these numbers automatically.

Note: As you can see this column contain strings as well as numbers but those numbers are read as strings.

Upvotes: 0

Views: 120

Answers (2)

Hamza
Hamza

Reputation: 6025

You can create a mask to check if they are numeric:

df[df[3].str.isnumeric()]

results in:

    3
4   90488
10  107147
16  134047

Upvotes: 0

Equinox
Equinox

Reputation: 6748

Convert it to numeric which will make invalid integers/floats into NaN. then drop the NaN and get the indices

pd.to_numeric(df['col'], errors='coerce').dropna().index

Out:

Int64Index([4, 10, 16], dtype='int64')

Upvotes: 1

Related Questions