1

I have multiple datasets that has the same number of rows and columns. The column is 0.1,2,3,4,5,6,7,8. For instance, Data1

0.1   3
2   3
3   0.1
4   10
5   5
6   7
7   9
8   2

Data2

0.1   2
2   1
3   0.1
4   0.5
5   4
6   0.3
7   9
8   2

I want to combine the data sets. However, I would like to combine the data by keeping the column and by adding the 2nd columns for multiple files.

0.1   3   2
2   3   1
3   0.1   0.1
4   10   0.5
5   5   4
6   7   0.3
7   9   9
8   2   2

I prefer to use Pandas Dataframe. Any clever way to go about this?

user7852656
  • 675
  • 4
  • 15
  • 26
  • I think merge is your answer https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html – run-out Feb 07 '19 at 01:37
  • 2
    Are you looking for merge/concat? https://stackoverflow.com/questions/53645882/pandas-merging-101 – cs95 Feb 07 '19 at 01:38
  • concat does not work. It will add the data vertically than horizontally. Also, it will repeat the first columns for each addition of the data.. – user7852656 Feb 07 '19 at 01:54

2 Answers2

0

Assuming the first column is the index and the second is data:

df = Data1.join(Data2, lsuffix='_1', rsuffix='_2')

Soumya Kanti
  • 1,429
  • 1
  • 17
  • 28
Mic
  • 1
  • 2
0

Or using merge, and setting column names as 'A' and 'B'

pd.merge(df1, df2, on='A',suffixes=('_data1','_data2'))

    A    B_data1    B_data2
0   0.1      3.0    2.0
1   2.0      3.0    1.0
2   3.0      0.1    0.1
3   4.0      10.0   0.5
4   5.0      5.0    4.0
5   6.0      6.0    0.3
6   7.0      9.0    9.0
7   8.0      2.0    2.0
run-out
  • 3,114
  • 1
  • 9
  • 25