问题
How can I filter only string values / integer / float values in one column in pandas data frame like below?
SIC
1 246804
2 135272
3 898.01
4 3453.33
5 shine
6 add
7 522
8 Nan
9 string
10 29.11
11 20
回答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
6 add
8 Nan
9 string
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
回答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
来源:https://stackoverflow.com/questions/45338209/filtering-string-float-interger-values-in-columns-pandas