鱼C论坛

 找回密码
 立即注册
查看: 2342|回复: 2

[已解决]能不能把df.info()中是object与其它的分开打印出来或提出来 ?请大师帮忙,谢谢

[复制链接]
发表于 2023-5-5 16:41:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
能不能把df.info()dtype中列名,是object与其它的分开打印出来或提出来 ?请大师帮忙,谢谢

import pandas as pd

data = [["2345", "A", "2022-12-20", "2.1016", "-0.72","2.8", "-0.77",78],
        ["004243", "C", "2022-12-15", "2.0891", ", 45","-0.77",98],
        ["519212", "B", "2022-12-10", "333", "1.9733", "-0.77", "-3.29",567],
        ["162411", "E", "2022-12-9", "0.7071", "-1.64","25", "-1.46",789],
        ["007844", "FC", "2022-11-19", "0.6967", '8', "88","-1.46",834.8],
        ["163208", "G", "2022-11-10", "1.0440", "-0.40","" ,"-0.40",0.568]]
columns = ["编号", "名称", "日期", "AA", "BB","CC","DD","EE"]
df = pd.DataFrame(data, columns=columns)

print(df.info())

object :      "AA", "BB","CC","DD"
float64 :    "EE"
最佳答案
2023-5-5 16:45:42
本帖最后由 歌者文明清理员 于 2023-5-5 16:47 编辑

可以使用df.select_dtypes()方法来实现筛选想要的数据类型。示例如下:

```
import pandas as pd

data = [["2345", "A", "2022-12-20", "2.1016", "-0.72","2.8", "-0.77",78],
        ["004243", "C", "2022-12-15", "2.0891", ", 45","-0.77",98],
        ["519212", "B", "2022-12-10", "333", "1.9733", "-0.77", "-3.29",567],
        ["162411", "E", "2022-12-9", "0.7071", "-1.64","25", "-1.46",789],
        ["007844", "FC", "2022-11-19", "0.6967", '8', "88","-1.46",834.8],
        ["163208", "G", "2022-11-10", "1.0440", "-0.40","" ,"-0.40",0.568]]
columns = ["编号", "名称", "日期", "AA", "BB","CC","DD","EE"]
df = pd.DataFrame(data, columns=columns)

# 输出所有列的数据类型
print(df.dtypes)

# 输出object类型的列名
print(df.select_dtypes(include=['object']).columns.tolist())

# 输出非object类型的列名
print(df.select_dtypes(exclude=['object']).columns.tolist())
```

输出结果如下所示:

```
编号      object
名称      object
日期      object
AA      float64
BB       object
CC       object
DD       object
EE      float64
dtype: object
['编号', '名称', '日期', 'BB', 'CC', 'DD']
['AA', 'EE']
```
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-5 16:45:42 | 显示全部楼层    本楼为最佳答案   
本帖最后由 歌者文明清理员 于 2023-5-5 16:47 编辑

可以使用df.select_dtypes()方法来实现筛选想要的数据类型。示例如下:

```
import pandas as pd

data = [["2345", "A", "2022-12-20", "2.1016", "-0.72","2.8", "-0.77",78],
        ["004243", "C", "2022-12-15", "2.0891", ", 45","-0.77",98],
        ["519212", "B", "2022-12-10", "333", "1.9733", "-0.77", "-3.29",567],
        ["162411", "E", "2022-12-9", "0.7071", "-1.64","25", "-1.46",789],
        ["007844", "FC", "2022-11-19", "0.6967", '8', "88","-1.46",834.8],
        ["163208", "G", "2022-11-10", "1.0440", "-0.40","" ,"-0.40",0.568]]
columns = ["编号", "名称", "日期", "AA", "BB","CC","DD","EE"]
df = pd.DataFrame(data, columns=columns)

# 输出所有列的数据类型
print(df.dtypes)

# 输出object类型的列名
print(df.select_dtypes(include=['object']).columns.tolist())

# 输出非object类型的列名
print(df.select_dtypes(exclude=['object']).columns.tolist())
```

输出结果如下所示:

```
编号      object
名称      object
日期      object
AA      float64
BB       object
CC       object
DD       object
EE      float64
dtype: object
['编号', '名称', '日期', 'BB', 'CC', 'DD']
['AA', 'EE']
```
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-5 16:46:05 | 显示全部楼层
您可以使用以下代码分别提取并打印对象类型(object)和非对象类型(非object)的列名:
import pandas as pd

data = [["2345", "A", "2022-12-20", "2.1016", "-0.72","2.8", "-0.77",78],
        ["004243", "C", "2022-12-15", "2.0891", ", 45","-0.77",98],
        ["519212", "B", "2022-12-10", "333", "1.9733", "-0.77", "-3.29",567],
        ["162411", "E", "2022-12-9", "0.7071", "-1.64","25", "-1.46",789],
        ["007844", "FC", "2022-11-19", "0.6967", '8', "88","-1.46",834.8],
        ["163208", "G", "2022-11-10", "1.0440", "-0.40","" ,"-0.40",0.568]]
columns = ["编号", "名称", "日期", "AA", "BB","CC","DD","EE"]
df = pd.DataFrame(data, columns=columns)

# 提取对象类型(object)和非对象类型(非object)的列名
object_columns = [col for col in df.columns if df[col].dtype == 'object']
non_object_columns = [col for col in df.columns if df[col].dtype != 'object']

# 打印结果
print("object: ", object_columns)
print("non-object: ", non_object_columns)

这将输出:
object:  ['编号', '名称', '日期', 'AA', 'BB', 'CC', 'DD']
non-object:  ['EE']

这样一来,您就可以看到哪些列是对象类型(object),哪些列是非对象类型(非object)。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-28 03:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表