简体   繁体   中英

Concatenation inside for loop pandas - only writing out variable for last element in list

list1 = [1,2,3,4,5,6]

for num1 in list1:
    pd.concat([dfnew[Indexer[0]],dfnew[Indexer[num1]]],axis=1)
  1. This returns a dataframe only for the last element in the list. How do I get dataframes for all elements in the list?
  2. Ideally I would also like to assign a name to each dataframe corresponding to each element in the list?

I have considered using a for loop but got lost on how to implement it successfully.

def test(list1):
    for num1 in list1:
     return pd.concat([dfnew[Indexer[0]],dfnew[Indexer[num1]]],axis=1)

I have made some progress but somehow I need to automate this so that I dont have to write this line 6 times while maintaining the unique variable names.

for num1 in list1:
    XReal=pd.concat([dfnew[Indexer[0]],dfnew[Indexer[1]]],axis=1)
    XImag=pd.concat([dfnew[Indexer[0]],dfnew[Indexer[2]]],axis=1)
    YReal=pd.concat([dfnew[Indexer[0]],dfnew[Indexer[3]]],axis=1)
    YImag=pd.concat([dfnew[Indexer[0]],dfnew[Indexer[4]]],axis=1)
    ZReal=pd.concat([dfnew[Indexer[0]],dfnew[Indexer[5]]],axis=1)
    ZImag=pd.concat([dfnew[Indexer[0]],dfnew[Indexer[6]]],axis=1)

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