将Pandas与共享数据合并

时间:2015-03-11 23:06:57

标签: python pandas merge

我有两个以下形式的数据框:

PID    Block    Trial    Response
101    A        1        1
101    A        2        2
102    A        1        1
102    A        2        1

PID    Trial    Fix    Dur
101    1        x      10
101    2        y      12
102    1        z      9
102    2        y      13

我需要将它们组合成一个如下所示的数据框:

PID    Block    Trial    Response    Fix    Dur 
101    A        1        1           x      10
101    A        2        2           y      12
102    A        1        1           z      9
102    A        2        1           y      13

我尝试过使用pd.merge(df1,df2,on = ['PID','Trial'],how ='outer',sort = False)。但这只是给出了这样的东西:

PID    Block    Trial    Response    Fix    Dur 
101    A        1        1           NaN    NaN
101    A        2        2           NaN    NaN
102    A        1        1           NaN    NaN
102    A        2        1           NaN    NaN
101    NaN      1        NaN         x      10
101    NaN      2        NaN         y      12
102    NaN      1        NaN         z      9
102    Nan      2        NaN         y      13

我对pd.join也没有任何好运。我也尝试使用(PID,Trial)元组创建一个新列,但是使用merge会产生与上面类似的结果。求救!

1 个答案:

答案 0 :(得分:0)

你想要一个内部联接:

>>> import pandas as pd
>>> df1 = pd.read_clipboard()
>>> df2 = pd.read_clipboard()
>>> df1
   PID Block  Trial  Response
0  101     A      1         1
1  101     A      2         2
2  102     A      1         1
3  102     A      2         1
>>> df2
   PID  Trial Fix  Dur
0  101      1   x   10
1  101      2   y   12
2  102      1   z    9
3  102      2   y   13
>>> pd.merge(df1, df2, on=['PID', 'Trial'], how='inner', sort=False)
   PID Block  Trial  Response Fix  Dur
0  101     A      1         1   x   10
1  101     A      2         2   y   12
2  102     A      1         1   z    9
3  102     A      2         1   y   13
>>>