Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas Dataframe merge and pick only few columns

I have a basic question on dataframe merge. After I merge two dataframe , is there a way to pick only few columns in the result.

Taking an example from documentation

https://pandas.pydata.org/pandas-docs/stable/merging.html#

left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],
                    'key2': ['K0', 'K1', 'K0', 'K1'],
                     'A': ['A0', 'A1', 'A2', 'A3'],
                     'B': ['B0', 'B1', 'B2', 'B3']})


right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],
                          'key2': ['K0', 'K0', 'K0', 'K0'],
                          'C': ['C0', 'C1', 'C2', 'C3'],
                          'D': ['D0', 'D1', 'D2', 'D3']})



result = pd.merge(left, right, on=['key1', 'key2'])

Result comes as :

    A   B key1 key2   C   D
0  A0  B0   K0   K0  C0  D0
1  A2  B2   K1   K0  C1  D1
2  A2  B2   K1   K0  C2  D2
None

Is there a way I can chose only column 'C' from 'right' dataframe? For example, I would like my result to be like:

    A   B key1 key2   C  
0  A0  B0   K0   K0  C0  
1  A2  B2   K1   K0  C1  
2  A2  B2   K1   K0  C2  
None
like image 796
ProgSky Avatar asked Jun 16 '17 15:06

ProgSky


1 Answers

result = pd.merge(left, right[['key1','key2','C']], on=['key1', 'key2'])

OR

right.merge(left, on=['key1','key2'])[['A','B','C','key1','key2']]
like image 200
Scott Boston Avatar answered Nov 15 '22 16:11

Scott Boston