Filtering String/float/integer Values In Pandas Dataframe Columns
How can I filter only string values/ integer/ float values in one column (SIC) in a pandas data frame like below? SIC 1 246804 2
Solution 1:
You can use the outputs from pd.to_numeric
and boolean indexing.
To get only the strings use:
df[pd.to_numeric(df.SIC, errors='coerce').isnull()]
Output:
SIC
5 shine
6add8 Nan
9string
To get only the numbers use:
df[pd.to_numeric(df.SIC, errors='coerce').notnull()]
Output:
SIC
1 246804
2 135272
3 898.01
4 3453.33
7 522
10 29.11
11 20
Solution 2:
You can use the apply()
method along with the isinstance()
function. Can replace str
with int
, float
, etc:
df = pd.DataFrame([1,2,4.5,np.NAN,'asdf',5,'string'],columns=['SIC'])
print(df)
SIC
0 1
1 2
2 4.5
3 NaN
4 asdf
5 5
6 string
print(df[df['SIC'].apply(lambda x: isinstance(x,str))])
SIC
4 asdf
6 string
Post a Comment for "Filtering String/float/integer Values In Pandas Dataframe Columns"