62 KiB
Combining DataFrames¶
Full Official Guide (Lots of examples!)¶
https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html¶
import numpy as np
import pandas as pd
Concatenation¶
Directly "glue" together dataframes.
data_one = {'A': ['A0', 'A1', 'A2', 'A3'],'B': ['B0', 'B1', 'B2', 'B3']}
data_two = {'C': ['C0', 'C1', 'C2', 'C3'], 'D': ['D0', 'D1', 'D2', 'D3']}
one = pd.DataFrame(data_one)
two = pd.DataFrame(data_two)
one
two
axis0 = pd.concat([one,two],axis=0)
axis0
axis1 = pd.concat([one,two],axis=1)
axis1
Axis 0 , but columns match up¶
In case you wanted this:
two.columns = one.columns
pd.concat([one,two])
registrations = pd.DataFrame({'reg_id':[1,2,3,4],'name':['Andrew','Bobo','Claire','David']})
logins = pd.DataFrame({'log_id':[1,2,3,4],'name':['Xavier','Andrew','Yolanda','Bobo']})
registrations
logins
pd.merge()¶
Merge pandas DataFrames based on key columns, similar to a SQL join. Results based on the how parameter.
help(pd.merge)
Inner,Left, Right, and Outer Joins¶
Inner Join¶
Match up where the key is present in BOTH tables. There should be no NaNs due to the join, since by definition to be part of the Inner Join they need info in both tables. Only Andrew and Bobo both registered and logged in.
# Notice pd.merge doesn't take in a list like concat
pd.merge(registrations,logins,how='inner',on='name')
# Pandas smart enough to figure out key column (on parameter) if only one column name matches up
pd.merge(registrations,logins,how='inner')
# Pandas reports an error if "on" key column isn't in both dataframes
# pd.merge(registrations,logins,how='inner',on='reg_id')
Left Join¶
Match up AND include all rows from Left Table. Show everyone who registered on Left Table, if they don't have login info, then fill with NaN.
pd.merge(registrations,logins,how='left')
Right Join¶
Match up AND include all rows from Right Table. Show everyone who logged in on the Right Table, if they don't have registration info, then fill with NaN.
pd.merge(registrations,logins,how='right')
Outer Join¶
Match up on all info found in either Left or Right Table. Show everyone that's in the Log in table and the registrations table. Fill any missing info with NaN
pd.merge(registrations,logins,how='outer')
Join on Index or Column¶
Use combinations of left_on,right_on,left_index,right_index to merge a column or index on each other
registrations
logins
registrations = registrations.set_index("name")
registrations
pd.merge(registrations,logins,left_index=True,right_on='name')
pd.merge(logins,registrations,right_index=True,left_on='name')
Dealing with differing key column names in joined tables¶
registrations = registrations.reset_index()
registrations
logins
registrations.columns = ['reg_name','reg_id']
registrations
# ERROR
# pd.merge(registrations,logins)
pd.merge(registrations,logins,left_on='reg_name',right_on='name')
pd.merge(registrations,logins,left_on='reg_name',right_on='name').drop('reg_name',axis=1)
Pandas automatically tags duplicate columns¶
registrations.columns = ['name','id']
logins.columns = ['id','name']
registrations
logins
# _x is for left
# _y is for right
pd.merge(registrations,logins,on='name')
pd.merge(registrations,logins,on='name',suffixes=('_reg','_log'))