python - 如何在 DataFrame 列中查找所有唯一数据类型?
问题描述
例如:
df = pd.DataFrame([[int(1),2,3],[int(5),6,'a'],[0.1,4,True]], columns = list("abc"))
df
a b c
0 1.0 2 3
1 5.0 6 a
2 0.1 4 True
(附带问题:为什么不声明int
做任何事情?)
现在,df.dtypes
回归
a float64
b int64
c object
dtype: object
但是是否有可能在一列中列出所有不同的数据类型?
喜欢
a int64, float64
b int64
c int64, str, bool
解决方案
您可以使用.applymap(type)
原始数据框中的各个值的类型来取回数据框:
df = pd.DataFrame([[1, 2, 3],[5, 6, 'a'],[0.1, 4, True]], columns = list("abc"))
print(df.applymap(type))
输出
a b c
0 <class 'float'> <class 'int'> <class 'int'>
1 <class 'float'> <class 'int'> <class 'str'>
2 <class 'float'> <class 'int'> <class 'bool'>
附带问题:为什么不声明
int
做任何事情?)
Python中没有“声明”这样的东西。int(1)
(几乎)没有什么不同1
。列a
显示为,float64
因为最后一个元素是0.1
。这会导致pandas
转换1
和5
到float
(因为0.1
不能转换为int
)。
推荐阅读
- git - 我可以通过团队资源管理器从开发分支变基到功能分支吗?
- mysql - MySQL - 'a bulk size specified must be increased' during copy database attempt
- reactjs - 如何在 React 应用程序中使用 JEST 测试向 api 发出 axios 请求的异步函数
- excel - Sort Combobox alphabetically vba excel
- c - Even after incrementing the value of the variable it won't change in the while loop
- go - dynamic data struct (nested json ) with dependency conditionals in go
- gradle - 在 JenkinsPipelineUnit 测试中生成代码覆盖率
- perl - 确定符号路径
- php - PHPmailer 在发送电子邮件时覆盖域
- reactjs - 在 ReactJS 中设置 Facebook Pixel 仅用于生产