在熊猫数据框中调用一对索引值时出现问题

时间:2020-01-28 18:04:38

标签: python pandas

我有一个列名列表,例如:

for c in collist:
    print(c)

返回

E1
E2
E3
E4
C1
C2
C3
C4
G1

我想遍历此列表和上面列表的每2个组合。

import itertools as itertools

for i in itertools.combinations(collist, 2):
    collist.append(i)

print(collist)
['E1', 'E2', 'E3', 'E4', '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'), ('E3', 'E4'), ('E3', 'C1'), ('E3', 'C2'), ('E3', 'C3'), ('E3', 'C4'), ('E3', 'G1'), ('E4', 'C1'), ('E4', 'C2'), ('E4', 'C3'), ('E4', 'C4'), ('E4', 'G1'), ('C1', 'C2'), ('C1', 'C3'), ('C1', 'C4'), ('C1', 'G1'), ('C2', 'C3'), ('C2', 'C4'), ('C2', 'G1'), ('C3', 'C4'), ('C3', 'G1'), ('C4', 'G1')]

问题是当我返回数据矩阵并调用collist时,它不起作用:

for col in collist:
    print(data[[col]])
KeyError: "None of [Index([('E1', 'E2')], dtype='object')] are in the [columns]"

我相信的问题是循环正在一起寻找('E1','E2'),bur却什么也没返回。

但是当我尝试独立运行时,它会起作用:

print(data[['E1','E2']])

我认为我需要对collist进行一些调整,以使索引值从数据帧data中读取。知道如何吗?

2 个答案:

答案 0 :(得分:0)

这将是另一种方式:

collist_2 = []
for i in range(len(collist)):
    try:
        collist_2.append([collist[i],collist[i+1]]) #Here we add the value and the following one.
    except IndexError: #This is to avoid raising an error when the index is out of range (the [i+1] for the final value of i)
        pass
final_list = collist + collist_2 #Concatenate both lists, the one with single values and the one with 2 values

现在,您可以像往常一样打印:

for col in final_list:
    print(df[col])

答案 1 :(得分:0)

您需要将列放在列表中。元组(A, B)不会查找列AB,它将查找名为(A, B)的列。

df = pd.DataFrame(np.random.randint(10, size=(5,10)))

cols = [list(x) for i in range (1,3) for x in itertools.combinations(df.columns, i)]

for col in cols:
    print(df[col])