Hariom Singh
Hariom Singh

Reputation: 3632

Pandas Combining data in two excel

I have two excel

Excel 1

files   language    blank   comment code
15       C++            66  35      354
1       C/C++ Header    3   7        4

Excel 2

files   language    blank   comment code
16       C++           33   35      354
1       C/C++ Header    3   7        4
1       Python          1   1        1

Trying to get combined excel

files   language    blank   comment code
31       C++           99   70      708
2       C/C++ Header    6   14       8
1       Python          1   1        1

Any tips in pandas

Upvotes: 1

Views: 44

Answers (2)

AYUSH SHARMA
AYUSH SHARMA

Reputation: 1

You can use


pd.merge(df1, df2, how='inner')

to merge

this is the easiest way to merge two or more DataFrames. For more information read the Pandas documentations. http://pandas.pydata.org/pandas-docs/stable/

Upvotes: 0

jezrael
jezrael

Reputation: 863116

Use concat with aggregate sum by groupby:

df = pd.concat([df1, df2]).groupby('language', as_index=False).sum()
print (df)
       language  files  blank  comment  code
0           C++     31     99       70   708
1  C/C++ Header      2      6       14     8
2        Python      1      1        1     1

If order of columns is important add reindex:

df=pd.concat([df1, df2]).groupby('language',as_index=False).sum().reindex(columns=df1.columns)
print (df)
   files      language  blank  comment  code
0     31           C++     99       70   708
1      2  C/C++ Header      6       14     8
2      1        Python      1        1     1

Upvotes: 1

Related Questions