Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Python Development Example
Merge Pandas DataFrame
Johan Louwers – Chief Customer Architect @ Oracle
Version : Feb 2019
@johanlouwers
Johanlouwers.blogspot.com
Oracle Confidential – Internal/Restricted/Highly Restricted
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
2
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. |
Python Development
with pandas and CX_Oracle
Slide-deck Intention :
• This presentation is intended to provide
a quick introduction example on how to
merge two DataFrames in Pandas
• This example is a part of a wider
workshop deck and shared as a stand-
alone example for ease of sharing.
• The code should only be used as an
educational example and is not
intended to be included in any real-
world application code.
3
Contact details :
Johan Louwers - Chief Customer Architect
@johanlouwers
Johan.louwers@oracle.com
github.com/louwersj
Johanlouwers.blogspot.com
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. |
A B C D
0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
4
A B C D
0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
A B C_x D_x C_y D_y
0 A0 B0 C0 D0 C0 D0
1 A1 B1 C1 D1 C1 D1
2 A2 B2 C2 D2 C2 D2
pd.read_csv
pd.read_csv
pd.merge
Python Development
Pandas – merge DataFrame
What do we want to achieve?
1) Load two sets of data from .csv files
2) “merge” them into a single dataframe
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. | 5
import pandas as pd
df0 = pd.read_csv('../../data/dataset_4.csv', delimiter=";",)
print ('show the content of the first file via dataframe df0')
print (df0.head())
df1 = pd.read_csv('../../data/dataset_5.csv', delimiter=";",)
print ('show the content of the second file via dataframe df1')
print (df1.head())
df2 = pd.merge(df0, df1, on=['Country Code','Country Name'])
print ('show the content of merged dataframes as a single dataframe')
print (df2.head())
Python Development
Pandas – merge DataFrame
Read first
CSV file
Read second
CSV file
Merge
DataFrames
Show me the code
1) Please note the 3 steps in the above code (two times read_csv() and one merge()
2) Find example at : https://github.com/louwersj/examples_machineLearning/blob/master/pandas/merging/pd_merge_example_1.py
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. | 6
Python Development
Pandas – merge DataFrame
Show me the output
1) Do note that the two “original” dataFrame objects contain each 63 columns
2) Do note that the “merged” DataFrame contains 124 columns and not 126 columns.
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. | Oracle Confidential – Internal/Restricted/Highly Restricted 7
Oracle python pandas merge DataFrames

Oracle python pandas merge DataFrames

  • 1.
    Copyright © 2016,Oracle and/or its affiliates. All rights reserved. | Python Development Example Merge Pandas DataFrame Johan Louwers – Chief Customer Architect @ Oracle Version : Feb 2019 @johanlouwers Johanlouwers.blogspot.com Oracle Confidential – Internal/Restricted/Highly Restricted
  • 2.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 2
  • 3.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | Python Development with pandas and CX_Oracle Slide-deck Intention : • This presentation is intended to provide a quick introduction example on how to merge two DataFrames in Pandas • This example is a part of a wider workshop deck and shared as a stand- alone example for ease of sharing. • The code should only be used as an educational example and is not intended to be included in any real- world application code. 3 Contact details : Johan Louwers - Chief Customer Architect @johanlouwers Johan.louwers@oracle.com github.com/louwersj Johanlouwers.blogspot.com
  • 4.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | A B C D 0 A0 B0 C0 D0 1 A1 B1 C1 D1 2 A2 B2 C2 D2 4 A B C D 0 A0 B0 C0 D0 1 A1 B1 C1 D1 2 A2 B2 C2 D2 A B C_x D_x C_y D_y 0 A0 B0 C0 D0 C0 D0 1 A1 B1 C1 D1 C1 D1 2 A2 B2 C2 D2 C2 D2 pd.read_csv pd.read_csv pd.merge Python Development Pandas – merge DataFrame What do we want to achieve? 1) Load two sets of data from .csv files 2) “merge” them into a single dataframe
  • 5.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | 5 import pandas as pd df0 = pd.read_csv('../../data/dataset_4.csv', delimiter=";",) print ('show the content of the first file via dataframe df0') print (df0.head()) df1 = pd.read_csv('../../data/dataset_5.csv', delimiter=";",) print ('show the content of the second file via dataframe df1') print (df1.head()) df2 = pd.merge(df0, df1, on=['Country Code','Country Name']) print ('show the content of merged dataframes as a single dataframe') print (df2.head()) Python Development Pandas – merge DataFrame Read first CSV file Read second CSV file Merge DataFrames Show me the code 1) Please note the 3 steps in the above code (two times read_csv() and one merge() 2) Find example at : https://github.com/louwersj/examples_machineLearning/blob/master/pandas/merging/pd_merge_example_1.py
  • 6.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | 6 Python Development Pandas – merge DataFrame Show me the output 1) Do note that the two “original” dataFrame objects contain each 63 columns 2) Do note that the “merged” DataFrame contains 124 columns and not 126 columns.
  • 7.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | Oracle Confidential – Internal/Restricted/Highly Restricted 7