With merge()
there is an indicator parameter so you can identify from which df you the original data is found. I don't see a similar function withconcat()
.
pd.merge(left=df1, right=df2, indicator=True)
Is there a way to create an indicator column with concat()
?
import pandas as pd
df1 = pd.DataFrame(['a','b'], index=[0,1], columns=['letter'])
df2 = pd.DataFrame(['c','d','e'], index=[2,3,4], columns=['letter'])
result = pd.concat([df1, df2], axis=0, keys=['from_df1','from_df2']).reset_index(level=[0])
df1
letter
0 a
1 b
df2
letter
2 c
3 d
4 e
result
level_0 letter
0 from_df1 a
1 from_df1 b
2 from_df2 c
3 from_df2 d
4 from_df2 e
I ended up creating a new column labeled FileName
in each file, then I concat()
.
df1['FileName'] = 'df1'
df2['FileName'] = 'df2'
final = pd.concat(df1,df2)
Column1 Column2 Column3 FileName
3 89 08 df1
43 934 34 df1
34 934 32 df2
431 2342 23 df2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With