Home > Net >  how do I convert list to stacked dataframe in Python which like do.call(rbind, list) in R
how do I convert list to stacked dataframe in Python which like do.call(rbind, list) in R

Time:10-16

how do I convert list to stacked dataframe in Python which like do.call(rbind, list) in R? I want to efficient way to code it

R code

    # create example data
    a1 <- data.frame(a = c(1, 2, 3), b = c(4, 5, 6))
    a2 <- data.frame(a = c(11, 22, 33), b = c(44, 55, 66))
    a3 <- data.frame(a = c(111, 222, 333), b = c(444, 555, 666))
    
    lst <- list()
    lst[1] <- a1
    lst[2] <- a2
    lst[3] <- a3
    
    lst # This data form is similar from my original data
    
    dat <- do.call(rbind, lst)
    dat <- data.frame(dat)

Python code (rough code)


    dfs = tabula.read_pdf(file, stream=True, pages='all', encoding='utf-8', guess = False, pandas_options={'header':None})
    
    df0 = dfs[0]
    df1 = dfs[1]
    df2 = dfs[2]
    df3 = dfs[3]
    
    dff = df0.append(df0, ignore_index=True)
    dff = dff.append(df1, ignore_index=True)
    dff = dff.append(df2, ignore_index=True)
    dff = dff.append(df3, ignore_index=True)


Python code (error, but try to write similar R code)

lst = []
df = dfs[for i in range(dfs) ] # error
dff = df.append(dfs[i]) # error

CodePudding user response:

Use pd.concat, or np.concatenate, depending on the data.

It will look roughly like this:

pd.concat([df0,df1,df,df3])

or just

pd.concat(dfs)
  • Related