简体   繁体   中英

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 ? 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.

If I understand you correctly, you want to iterate over rows and don't want to specify variables in for-loop. You can use .itertuples() then:

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

Prints:

FX A 1
IR A 2
IR L 3

Or .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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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