Filtering string/float/interger values in columns (Pandas)

China☆狼群 提交于 2020-06-27 08:29:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!