简体   繁体   English

使用变量迭代 Pandas 中的列

[英]Iterating over columns in pandas with variables

I have a dataframe that looks like:我有一个看起来像的数据框:

import pandas as pd
f = {'business':['FX','IR','IR'],'level':['A','A','L'],'amt':[1,2,3]}
df1 = pd.DataFrame(data=f)
df1

I have loop that iterates over the columns of the dataframe by creating variables:我有一个循环,它通过创建变量来遍历数据框的列:

for business_v,level_v,amt in list(zip(*[df1[col]for col in df1])):
    print (business_v,levelv,amt)

Rather than defining three variables business_v , level_v , and amt , is there a way to automatically create variables based on the number of columns in the dataframe df1 ?有没有办法根据数据帧df1的列数自动创建变量,而不是定义三个变量business_vlevel_vamt My dataframe changes in size, and I wanted to create variables based on the size of the dataframe.我的数据框大小发生变化,我想根据数据框的大小创建变量。

Instead of creating an arbitrary number of variables, you can consider using indexing as follows:您可以考虑使用索引,而不是创建任意数量的变量,如下所示:

for idx in range(df1.shape[0]):
    print ([df1.iloc[i][col] for col in df1.columns()])

You can use a custom set of columns instead of calling df1.columns() if you only want to use a subset of columns in the subsequent code.如果您只想在后续代码中使用列的子集,则可以使用自定义列集而不是调用 df1.columns()。

If I understand you correctly, you want to iterate over rows and don't want to specify variables in for-loop.如果我理解正确,您想遍历行并且不想在 for 循环中指定变量。 You can use .itertuples() then:您可以使用.itertuples()然后:

for t in df1.itertuples():
    print(t.business, t.level, t.amt)

Prints:印刷:

FX A 1
IR A 2
IR L 3

Or .iterrows() :.iterrows()

for idx, t in df1.iterrows():
    print(idx, t[0], t[1], t[2])

Prints:印刷:

0 FX A 1
1 IR A 2
2 IR L 3
for i in df1.itertuples():
    print(i[1],i[2],i[3])

FX A 1
IR A 2
IR L 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM