Pandas:遍历列并从一列开始

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51408351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:49:12  来源:igfitidea点击:

Pandas: Iterate through columns and starting at one column

pythonpandasdataframe

提问by bobo T

Does anyone know how to incorporate a forloop for columns but start at any column? (The third one for this scenario)

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

Lets say this is the dataframe:

假设这是数据框:

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

I want to iterate through skice, bike, dike, and mike only

我想通过迭代skice,自行车,堤防和Mike

回答by harvpan

Since df.columnsprovides you the list of columns, you can do below and iterate from 3rd column name.

由于df.columns为您提供了列列表,您可以在下面执行并从第 3 列名称进行迭代。

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

回答by Joe

More general solutions if the columns are not consecutive:

如果列不连续,则更通用的解决方案:

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