简体   繁体   中英

Ways to name multiple pandas datafreames after joining 2 dataframes

I have two large dataframes, what I want to do is make lots of smaller dataframaes consisting of the first dataframe joined with the first column of the second data frame. I have done this for one instance, however I am struggling to find the best way of storing all of the dataframes to be produced.

Here is the line of code for one instance

test=pd.concat([rxloc,RX.iloc[:,0]],axis=1)

where rxloc is my first dataframe and RX is my second dataframe

This works great, but I need to do this on 1377 columns (from the second dataframe). I can produce a loop to do the process, however Im having trouble with how to simply and effectively name all of the new dataframes in a loop. Im still pretty new to python and pandas so any help is greatly appreciated.

Update: This initial question has been answered, however I now want to repeat this process by appending another dataframe to one created by this line of code.

for i in range(len(list(RX))): all_dfs.append(pd.concat([rxloc,RX.iloc[:,i]],axis=1))

Im having some trouble with the appropriate loop here, again I want to add one column of dataframe 2 to each dataframe in the list all_dfs. Again any help is hugely appreciated.

You can use a loop indeed to append each dataframe to a list:

all_dfs = []
for i in range(len(list(RX))):
    all_dfs.append(pd.concat([rxloc,RX.iloc[:,i]],axis=1))

And each value in the list all_dfs will be a different dataframe. Furthermore, the index-value of the dataframe in all_dfs will match with the position of the column in RX (in case you need to make some kind of future reference). Also, I'm using range(len()) instead of simply list(RX) in case there are columns with equal names.

你可以使用字典理解

all_data ={df{}.format(x):pd.concat([rxloc,RX.iloc[:,x]],axis=1) for x in range(len(RX.columns))}

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