python - Pandas 使用值计数获取类型

标签 python pandas

# col is a series
t = col.apply(type).value_counts()
t_count = zip(t.index, t)

我的结果是t.index<type 'int'> 。我如何确定这个类型是int。基本上我想要这样的东西

<type 'int'> == type(int) #It is returning false currently but i would like it to return true without converting it to a str and processing it

最佳答案

您可以与int进行比较:

col =  pd.Series([1,2,'a','d','d',1.5,7.8])
t = col.apply(type).value_counts() 
print (t)
<class 'str'>      3
<class 'float'>    2
<class 'int'>      2
dtype: int64

print (t.index == int)
[False False  True]

print (t[t.index == int])
<class 'int'>    2
dtype: int64

也可以比较系列:

print (col.apply(type) == int)
0     True
1     True
2    False
3    False
4    False
5    False
6    False
dtype: bool

print (col[col.apply(type) == int])
0    1
1    2
dtype: object

关于python - Pandas 使用值计数获取类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45283449/

相关文章:

python - 如何通过 ssh 进入服务器,并使用 python 在该服务器上执行 bash 命令

Python Pandas 动态创建数据框

Python Pandas,将表格更改为百分比而不是单个结果

python - 根据列名对 Pandas 数据框中的列进行排序

python - Pandas DataFrame 列到数据透视表中的单元格

Python Qlistview 输出目录

python - 如何在 Python 中将 X509Name 对象转换为字符串

python - 如何在 def 函数中使用 tkinter 从 url 发布图像

python - 在 Pandas 中剥离时区信息

python - 如何优雅地替换字符串中的每个元素(字符)?