0

I am new to python and pandas. I want to create all combinations from two dataframe such as follows, where I will have all values for each Name and date combinations... anyone please could help?

I need a dataframe such that for each Name and date(Unique Key) in first dataframe will have all the values mentioned in second dataframe for that particular matching date.

first_df =
|  date | Name | ExtraCol
| 01/01 | AB   |  abc
| 01/01 | CD   |  def
| 01/02 | AB   |  xyz

second_df = 
|  date | value
| 01/01 | 1
| 01/01 | 2
| 01/01 | 3
| 01/01 | 4
| 01/02 | 1
| 01/02 | 2

expected_df = 
| date | Name | Value | ExtraCol
| 01/01| AB   | 1     | abc
| 01/01| AB   | 2     | abc
| 01/01| AB   | 3     | abc
| 01/01| AB   | 4     | abc
| 01/01| CD   | 1     | def
| 01/01| CD   | 2     | def
| 01/01| CD   | 3     | def
| 01/01| CD   | 4     | def
| 01/02| AB   | 1     | xyz
| 01/02| AB   | 2     | xyz
Jay Patel
  • 49
  • 4

1 Answers1

0

Use merge with outer join:

>>> df2.merge(df1, on='date', how='outer')
    date  value Name ExtraCol
0  01/01      1   AB      abc
1  01/01      1   CD      def
2  01/01      2   AB      abc
3  01/01      2   CD      def
4  01/01      3   AB      abc
5  01/01      3   CD      def
6  01/01      4   AB      abc
7  01/01      4   CD      def
8  01/02      1   AB      xyz
9  01/02      2   AB      xyz

Read Pandas Merging 101

Corralien
  • 109,409
  • 8
  • 28
  • 52