检查数据序列是否为字符串 [英] Checking if a data series is strings
本文介绍了检查数据序列是否为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查数据框中的一列是否包含字符串.我本以为可以仅通过检查dtype来完成此操作,但事实并非如此.包含字符串的pandas系列仅具有dtype'object',它也用于其他数据结构(如列表):
I want to check if a column in a dataframe contains strings. I would have thought this could be done just by checking dtype, but that isn't the case. A pandas series that contains strings just has dtype 'object', which is also used for other data structures (like lists):
df = pd.DataFrame({'a': [1,2,3], 'b': ['Hello', '1', '2'], 'c': [[1],[2],[3]]})
df = pd.DataFrame({'a': [1,2,3], 'b': ['Hello', '1', '2'], 'c': [[1],[2],[3]]})
print(df['a'].dtype)
print(df['b'].dtype)
print(df['c'].dtype)
产生:
int64
object
object
是否可以通过某种方式检查列是否仅包含字符串?
Is there some way of checking if a column contains only strings?
推荐答案
您可以使用它来查看列中的所有元素是否都是字符串
You can use this to see if all elements in a column are strings
df.applymap(type).eq(str).all()
a False
b True
c False
dtype: bool
只检查是否有字符串
df.applymap(type).eq(str).any()
这篇关于检查数据序列是否为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文