Pandas:遍历列并从一列开始

2024-06-16 13:06:23 发布

您现在位置:Python中文网/ 问答频道 /正文

有人知道如何为列合并forloop,但从任何列开始吗?(本场景的第三个)

假设这是数据帧:

spice smice skice bike dike mike 
1     23     35    34   34   56 
135   34     23    21   56   34
231   12     67    21   62   75

我只想遍历滑雪板、自行车、堤坝和麦克


Tags: 数据场景自行车mikespicebikeforloopskice
2条回答

如果列不连续,则提供更一般的解决方案:

l = ['skice', 'bike', 'dike','mike']
cols = df.columns.intersection(l)
for col in cols:
    print (col)

由于df.columns为您提供了列的列表,因此您可以执行以下操作并从第三个列名开始迭代。

for col in df.columns[2:]:
    #print(df[col].unique())

相关问题 更多 >