gpt4 book ai didi

python - 数据帧 : looping through calling some variables while manually calling others

转载 作者:行者123 更新时间:2023-12-01 06:29:28 25 4
gpt4 key购买 nike

我有一个数据框data,它包含多个名为:

 y        E4        C1  ...  season_2  season_3      ar1

我有列表collistful,它包含数据帧数据中一些变量的各种组合:

['E1'],
['C1'],
['C2'],
['C3'],
['C4'],
['G1'],
['E1', 'E2'],
['E1', 'E3'],
['E1', 'E4'],
['E1', 'C1'],
['E1', 'C2'],
['E1', 'C3'],
['E1', 'C4'],
['E1', 'G1'],
['E2', 'E3'],
['E2', 'E4'],
['E2', 'C1'],
['E2', 'C2'],
['E2', 'C3'],
['E2', 'C4'],
['E2', 'G1'],

我想做的是编写一个循环,调用/循环 collistfull 中的变量,同时还手动调用每个循环中包含的某些变量,例如那:

for col in collistfull: 
datanew = data[(col),"y","season_2","ar1"]
print(datadrop1)

但是我上面尝试的代码返回错误:TypeError: '(['E1'], 'y', 'season_2', 'ar1')' is an invalid key

最佳答案

您可以简单地执行以下操作:

for col in collistfull: 
datanew = data[col + ["y","season_2","ar1"]]
print(datanew)

由于每个 col 都是一个列表,我们可以简单地使用 + 运算符将新名称附加到 col 中。

关于python - 数据帧 : looping through calling some variables while manually calling others,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59974386/

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