-1

I have two pandas data frames of the same size (same rows and columns) and wish to join them so that the resulting data frame has two values in each cell (coming from the two initial data frames). The first data frame contains integers and the second data frame contains floats. Is there a way to do this? Thank you.

I've looked into pd.merge(), pd.join(), and pd.concat() but wasn't able to get them to solve my problem.

  • GIve us an example of how the _inputs_ and the _output_ should look like. – Yannis P. Mar 14 '23 at 19:29
  • What is your data what is your expected result please share more detail we can not help you like that. – Elkhan Mar 14 '23 at 19:34
  • Welcome to stack overflow! Please have a look at [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and [edit] your question to include a [mcve] with a sample of your input, your expected output, and code for what you've tried so far – G. Anderson Mar 14 '23 at 19:40

1 Answers1

0

You can use the agg method:

>>> df = pd.DataFrame({'A': [5, 4, 1], 'B': [1.0, 2.0, 3.0]}, index=[0, 1, 2])  
>>> df.agg(lambda x: (x['A'],x['B']), axis=1) 
0    (5.0, 1.0)
1    (4.0, 2.0)
2    (1.0, 3.0)
dtype: object

This can also be done with two separate dataframes, if one is creative with the index:

>>> df
   A    B
0  5  1.0
1  4  2.0
2  1  3.0
>>> df1
   C
0  3
1  5
2  6
>>> df.reset_index(drop=False).agg(lambda x: (int(x['A']), df1.loc[x['index'], 'C']), axis=1)
0    (5, 3)
1    (4, 5)
2    (1, 6)
dtype: object
HansQ
  • 380
  • 1
  • 7