<jupyter_start><jupyter_text>Merging, Joining, and ConcatenatingThere are 3 main ways of combining DataFrames together: Merging, Joining and Concatenating. In this lecture we will discuss these 3 methods with examples.____  Example DataFrames<jupyter_code>import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                        'B': ['B0', 'B1', 'B2', 'B3'],
                        'C': ['C0', 'C1', 'C2', 'C3'],
                        'D': ['D0', 'D1', 'D2', 'D3']},
                        index=[0, 1, 2, 3])
df1
df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
                        'B': ['B4', 'B5', 'B6', 'B7'],
                        'C': ['C4', 'C5', 'C6', 'C7'],
                        'D': ['D4', 'D5', 'D6', 'D7']},
                         index=[4, 5, 6, 7])
df2
df3 = pd.DataFrame({'A': ['A8', 'A9', 'A10', 'A11'],
                        'B': ['B8', 'B9', 'B10', 'B11'],
                        'C': ['C8', 'C9', 'C10', 'C11'],
                        'D': ['D8', 'D9', 'D10', 'D11']},
                        index=[8, 9, 10, 11])
df3<jupyter_output><empty_output><jupyter_text>Read the above dataframes.  ConcatenationConcatenation basically glues together DataFrames. Keep in mind that dimensions should match along the axis you are concatenating on.  Q1. Concatenate the df1,df2 & df3 dataframes vertically. Q2. Concatenate the df1,df2 & df3 dataframes horizontally.<jupyter_code>##Start code here
pd.concat([df1,df2,df3],axis=1)
##End 
##Start code here
pd.concat([df1,df2,df3],axis=0)
##End<jupyter_output><empty_output><jupyter_text>_____ Example DataFrames<jupyter_code>left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
                     'A': ['A0', 'A1', 'A2', 'A3'],
                     'B': ['B0', 'B1', 'B2', 'B3']})
   
right = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
                          'C': ['C0', 'C1', 'C2', 'C3'],
                          'D': ['D0', 'D1', 'D2', 'D3']})   
print(left)
print(right)<jupyter_output>key   A   B
0  K0  A0  B0
1  K1  A1  B1
2  K2  A2  B2
3  K3  A3  B3
  key   C   D
0  K0  C0  D0
1  K1  C1  D1
2  K2  C2  D2
3  K3  C3  D3<jupyter_text>Read the above left & right data.   MergingThe **merge** function allows you to merge DataFrames together using a similar logic as merging SQL Tables together. For example: Q1. Merge the dataframe left & right keeping the merge to inner. Q2. Merge the dataframe left & right keeping the merge to outer.Q3. Merge the dataframe left & right keeping the merge to left.Q4. Merge the dataframe left & right keeping the merge to right.<jupyter_code>##Start code here
pd.merge(left,right,how='inner')
##End 
##Start code here
pd.merge(left,right,how='outer')
##End 
##Start code here
pd.merge(left,right,how='left')
##End 
##Start code here
pd.merge(left,right,how='right')
##End<jupyter_output><empty_output><jupyter_text>JoiningJoining is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame.<jupyter_code>left = pd.DataFrame({'A': ['A0', 'A1', 'A2'],
                     'B': ['B0', 'B1', 'B2']},
                      index=['K0', 'K1', 'K2']) 

right = pd.DataFrame({'C': ['C0', 'C2', 'C3'],
                    'D': ['D0', 'D2', 'D3']},
                      index=['K0', 'K2', 'K3'])
print(left)
print(right)<jupyter_output>A   B
K0  A0  B0
K1  A1  B1
K2  A2  B2
     C   D
K0  C0  D0
K2  C2  D2
K3  C3  D3<jupyter_text>Q1. Join the dataframe left & right keeping the joining operation to inner. Q2. Join the dataframe left & right keeping the joining operation to outer. Q3. Join the dataframe left & right keeping the joining operation to right. Q4. Join the dataframe left & right keeping the joining operation to left.<jupyter_code>##Start code here
left.join(right,how='inner')
##End 
##Start code here
left.join(right,how='outer')
##End 
##Start code here
left.join(right,how='right')
##End 
##Start code here
left.join(right,how='left')
##End<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16604244694132334, 'fraction_numerical': 0.0484394506866417, 'mean_word_length': 2.811607992388202, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '232253', 'license': 'Apache-2.0', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1476, 'n_words': 443, 'path': 'pandas 149.ipynb', 'repo_name': 'Akash-tirupati/pandas', 'size': '4005'}