Reputation: 132
For example, I've got this data frame.
import pandas as pd
import numpy as np
data = {'state': ['Ohio', 'Florida', 'Texas', 'Washington', 'Arizona', 'Nevada'],
'year' : [2000, 2000, 2000, 2000, 2000, 2000],
'event': [1.5, "None", 3.6, 2.4, "None", 3.2]}
frame = pd.DataFrame(data)
frame
OUT>>
state year event
0 Ohio 2000 1.5
1 Florida 2000 None
2 Texas 2000 3.6
3 Washington 2000 2.4
4 Arizona 2000 None
5 Nevada 2000 3.2
Question/ How to filter the event column by display the DataFrame with only the float type based on the event column?
Looking the result to be like
state year event
0 Ohio 2000 1.5
1 Texas 2000 3.6
2 Washington 2000 2.4
3 Nevada 2000 3.2
Upvotes: 2
Views: 1048
Reputation: 2670
Try:
frame[pd.to_numeric(frame.event, errors='coerce').notnull()]
Or even:
frame.query("event != 'None'")
Outputs:
state year event
0 Ohio 2000 1.5
2 Texas 2000 3.6
3 Washington 2000 2.4
5 Nevada 2000 3.2
Upvotes: 1
Reputation: 378
You can create a subset of the dataframe by checking which values are float in the 'event' column. The reset index is to renumber the index rows.
frame[frame['event'].apply(lambda x: isinstance(x,float))].reset_index(drop=True)
Upvotes: 1