Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to append pandas dataframe at once

Is there a way to append all pandas dataframe at once. For example, I have below

df['A'] 
col1  col2
 1     x
 2     y

df['B'] 
col1  col2
 1     x5
 2     y2

df['F'] 
col1  col2
 13    x3
 2     y3
.
.
.
.
.
df['H']
col1  col2
 13    x1
 23    y2

I have many dataframe with same columns and with different variable name. Is there a way to append this along with variable name in the columns

Expected output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

File  col1    col2 
 A     1       x
 A     2       y
 B     1       x5
 B     2       y2
 F     13      x3
 F     2       y3
 .     .       .
 .     .       .
 H     13      x1
 H     23      y2

>Solution :

You can use pd.concat

>>> import pandas as pd
>>> A = pd.DataFrame({"col1": [1, 2], "col2":["x","y"]})
>>> B = pd.DataFrame({"col1": [1, 2], "col2": ["x5", "y2"]})
>>> A["File"] = "A"
>>> B["File"] = "B"
>>> pd.concat([A,B])
   col1 col2 File
0     1    x    A
1     2    y    A
0     1   x5    B
1     2   y2    B

For multiple dfs, one can use

dfs = []
for file_col in file_cols:
    newdf = df[file_col]
    newdf["File"] = file_col
    dfs.append(newdf)
pd.concat(dfs)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading