pd.merge 2つのdataframeをマージ
1158 ワード
mergeは2つのdataframeを結合し、
import pandas as pd
>>> df1=pd.DataFrame({
'a':[0,0,0,0,1,1,1,1],
'b':[0,0,1,1,0,0,1,1] ,
'c':[1,2,3,4,5,6,7,8],
'd':['h','i','j','k','l','m','n','o']
})
>>> df2=pd.DataFrame({
'a':[0,0,1,1],
'b':[0,1,0,1] ,
'e':[4,5,6,7],
'f':['x','y','z','z']
})
>>> df1
a b c d
0 0 0 1 h
1 0 0 2 i
2 0 1 3 j
3 0 1 4 k
4 1 0 5 l
5 1 0 6 m
6 1 1 7 n
7 1 1 8 o
>>> df2
a b e f
0 0 0 4 x
1 0 1 5 y
2 1 0 6 z
3 1 1 7 z
>>> df = df1.merge(df2,on=['a','b'])
>>> df
a b c d e f
0 0 0 1 h 4 x
1 0 0 2 i 4 x
2 0 1 3 j 5 y
3 0 1 4 k 5 y
4 1 0 5 l 6 z
5 1 0 6 m 6 z
6 1 1 7 n 7 z
7 1 1 8 o 7 z
>>> df_one = df[['a','c','f']]
>>> df_one
a c f
0 0 1 x
1 0 2 x
2 0 3 y
3 0 4 y
4 1 5 z
5 1 6 z
6 1 7 z
7 1 8 z
>>>