Reputation: 57
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.
Upvotes: 2
Views: 56
Reputation: 4872
u can use dictionary comprehension
all_data ={df{}.format(x):pd.concat([rxloc,RX.iloc[:,x]],axis=1) for x in range(len(RX.columns))}
Upvotes: 1
Reputation: 18367
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.
Upvotes: 2