简体   繁体   English

python pandas-检查列中是否存在字符串类型

[英]python pandas - check if a string type exists in a column

I know how to check for a 'nan' value in in column 'A' of dataframe 'df' as follows 我知道如何在数据框“ df”的“ A”列中检查“ nan”值,如下所示

df['A'].isnull().values.any()

but how can I check for a 'string', and I mean any string, since i do not know what the string text is, and then also to know which row it was found in? 但是如何检查“字符串”,我是指任何字符串,因为我不知道字符串文本是什么,然后还知道在其中找到哪一行?

If you are using Python 3, you can use a list comprehension and numpy.any 如果您使用的是Python 3,则可以使用列表推导和numpy.any

 import numpy as np

 np.any([isinstance(val, str) for val in df['A']])

If you are using Python 2, I believe that you need to replace str with basestring. 如果您使用的是Python 2,我相信您需要用basestring替换str。

I would use vectorized Pandas approach: 我将使用矢量化熊猫方法:

Assuming we have the following DF: 假设我们有以下DF:

In [116]: df = pd.DataFrame({'a':[1,2,'aaa', 3.14, 2.71], 'b':['2016-01-01', 'bbb', '2016-02-02', '2016-03-03', 'ZZZ']})

In [117]: df
Out[117]:
      a           b
0     1  2016-01-01
1     2         bbb
2   aaa  2016-02-02
3  3.14  2016-03-03
4  2.71         ZZZ

In [118]: df.dtypes
Out[118]:
a    object
b    object
dtype: object

check for strings in the column that supposed to be numeric : 检查列中应该为数字的字符串:

In [119]: pd.to_numeric(df.a, errors='coerce')
Out[119]:
0    1.00
1    2.00
2     NaN
3    3.14
4    2.71
Name: a, dtype: float64

In [120]: pd.to_numeric(df.a, errors='coerce').isnull()
Out[120]:
0    False
1    False
2     True
3    False
4    False
Name: a, dtype: bool

In [121]: df.loc[pd.to_numeric(df.a, errors='coerce').isnull()]
Out[121]:
     a           b
2  aaa  2016-02-02

check for strings in the column that supposed to be datetime-like : 检查应该类似于datetime的列中的字符串:

In [122]: pd.to_datetime(df.b, errors='coerce')
Out[122]:
0   2016-01-01
1          NaT
2   2016-02-02
3   2016-03-03
4          NaT
Name: b, dtype: datetime64[ns]

In [123]: df.loc[pd.to_datetime(df.b, errors='coerce').isnull()]
Out[123]:
      a    b
1     2  bbb
4  2.71  ZZZ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM