pd.merge没有像往常一样工作

时间:2018-06-11 12:47:35

标签: python-3.x pandas dataframe merge left-join

所有

我有两个数据帧:allHoldings和Longswap

allHoldings

   prime_broker_id    country_name position_type
0         CS         UNITED STATES          LONG
1         ML         UNITED STATES          LONG
2         CS               AUSTRIA         SHORT
3         HSBC              FRANCE          LONG
4         CITI       UNITED STATES         SHORT
11        DB         UNITED STATES         SHORT
12        JPM        UNITED STATES         SHORT
13        CS                 ITALY         SHORT
14        CITI              TAIWAN         SHORT
15        CITI      UNITED KINGDOM          LONG
16        DB                FRANCE          LONG
17        ML           SOUTH KOREA          LONG
18        CS               AUSTRIA         SHORT
19        CS                 JAPAN          LONG
26        HSBC              FRANCE         SHORT

和Longswap

   prime_broker_id          country_name  longSpread
0                ML             AUSTRALIA        30.0
1                ML               AUSTRIA        30.0
2                ML               BELGIUM        30.0
3                ML                BRAZIL        50.0
4                ML                CANADA        20.0
5                ML                 CHILE        50.0
6                ML             CHINA - A        75.0
7                ML        CZECH REPUBLIC        45.0
8                ML               DENMARK        30.0
9                ML                 EGYPT        45.0
10               ML               FINLAND        30.0
11               ML                FRANCE        30.0
12               ML               GERMANY        30.0
13               ML             HONG KONG        30.0
14               ML               HUNGARY        45.0
15               ML                 INDIA        75.0
16               ML             INDONESIA        75.0
17               ML               IRELAND        30.0
18               ML                ISRAEL        45.0
19               ML                 ITALY        30.0
20               ML                 JAPAN        30.0
21               ML           SOUTH KOREA        50.0
22               ML            LUXEMBOURG        30.0
23               ML              MALAYSIA        75.0
24               ML                MEXICO        50.0
25               ML           NETHERLANDS        30.0
26               ML           NEW ZEALAND        30.0
27               ML                NORWAY        30.0
28               ML           PHILIPPINES        75.0

我之前已经加入了很多数据帧,但我仍然感到困惑的是为什么它不能用于这个例子。

这是我的代码:

allHoldings=pd.merge(allHoldings, Longswap, how='left', left_on = ['prime_broker_id','country_name'], right_on=['prime_broker_id','country_name'])

我的结果是

  prime_broker_id    country_name position_type  longSpread
0        CS         UNITED STATES          LONG         NaN
1        ML         UNITED STATES          LONG         NaN
2        CS               AUSTRIA         SHORT         NaN
3        HSBC              FRANCE          LONG         NaN
4        CITI       UNITED STATES         SHORT         NaN
5        DB         UNITED STATES         SHORT         NaN
6        JPM        UNITED STATES         SHORT         NaN
7        CS                 ITALY         SHORT         NaN

你可以看到longSpread列是一个没有任何意义的NaN。从longSwap数据框中,应该填充此列。

我不确定为什么左连接不能在这里工作。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这是删除空格并使左连接成功的答案

allHoldings.prime_broker_id.str.strip()

array(['CS', 'ML', 'HSBC', 'CITI', 'DB', 'JPM', 'WFPBS'], dtype=object)