gpt4 book ai didi

python - 生成 Pandas 数据框中所有列的列表

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:49 25 4
gpt4 key购买 nike

我的数据框有 40 多列。我想生成列表,每个列表包含一列中的值。这是我尝试做的方法

cols= df.columns
cols = cols.tolist()
for col in cols:
col = df.col.tolist()

错误:

'DataFrame' object has no attribute 'col'

所以看起来循环试图在数据框中找到“col”,而不是从列表“cols”中获取项目。

使用

 A = df.A.tolist()

有效,所以我猜我在循环中犯了错误?

最佳答案

使用 [] 按列名进行选择:

for col in cols:
col = df[col].tolist()

如果需要列表中的所有值都是可能的,请按 DataFrame.to_dict 创建字典:

d = df.to_dict(orient='list')

关于python - 生成 Pandas 数据框中所有列的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55824345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com