futui 发表于 2023-5-5 16:41:35

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

能不能把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']
```

isdkz 发表于 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 = .dtype == 'object']
non_object_columns = .dtype != 'object']

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


这将输出:

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


这样一来,您就可以看到哪些列是对象类型(object),哪些列是非对象类型(非object)。
页: [1]
查看完整版本: 能不能把df.info()中是object与其它的分开打印出来或提出来 ?请大师帮忙,谢谢