Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an indicator column for concat in pandas?

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

like image 763
Martin Bobak Avatar asked Mar 06 '23 13:03

Martin Bobak


2 Answers

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
like image 65
Juan Kania-Morales Avatar answered Mar 15 '23 19:03

Juan Kania-Morales


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
like image 23
Martin Bobak Avatar answered Mar 15 '23 18:03

Martin Bobak