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
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)