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
Pandas: Iterate through columns and starting at one column
提问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.columns
provides 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)