Neeraja Bandreddi
Neeraja Bandreddi

Reputation: 437

rbindlist equivalent R's function in python

First i am creating empty lists based on length of the num_vars and storing the output of each loop in one list. After that I want to combine the all the outputs and convert that as pandas data frame.

for this we can simply use rbindlist in R, for combine the list objects.

for that i used the following python code.

ests_list=[[] for i in range(num_vars)]

for i in list(range(0,num_vars)):
    for j in list(range(1,num_vars+1))
        ests_list[i]=pd.merge(df1,
                              df2,
                              how='left',
                              on=eval('combine%s'%j+'_lvl'))

pd.concat(ests_list)

when i tried the above syntax it throws the following error:

TypeError: cannot concatenate object of type "<class 'list'>"; only 
pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid

Please anyone can help me to solve this issue.

Thanks in advance.

Upvotes: 2

Views: 2527

Answers (1)

Neeraja Bandreddi
Neeraja Bandreddi

Reputation: 437

I found a solution for my problem:

ests_list=[]
for i in list(range(1,num_vars)):
    ests_list.append(df1.merge(df2,how='left',on=eval("combine%s"%i+"_lvl")))
pd.concat(ests_list)

I am creating an empty list and and I added each loop output to it.

Then I am combining all the list by using the pd.concat function, so it gives me the output in pandas data frame format.

Upvotes: 2

Related Questions