Pandas合并和连接无法正常工作

3

我有一个合并两个数据框的问题。

我正在处理由相同的SQL数据库和CSV文件创建的10个数据框对的列表。

在一些数据框对中,merge(df1, df2)能够正确地工作,但是df1.join(df2)不能。例如,这是其中一个数据框对的子集:

>>> a
         mod:user studentid
2010453   3891583   2010453
2112086    890910   2112086
2222220    201611   2222220
2346979      7084   2346979
2414996   1817436   2414996
2420317     52821   2420317
2438767    884012   2438767
2451924  20815145   2451924
2515531   2115829   2515531
2536751    494565   2536751
2549050    315295   2549050
2549530         0   2549530
2551532    544968   2551532
2551542       213   2551542
2610206   1257038   2610206
2624429    939670   2624429
2630017         6   2630017
2633815    190564   2633815
2633857   1147211   2633857
2634405   1093092   2634405
2641370   2038012   2641370
2644284    658743   2644284
2649427    220230   2649427
2712372      9468   2712372
2714617   1231577   2714617
2718450   3907345   2718450
2732910         0   2732910
2739711    396876   2739711
8200703       166   8200703
9906492    920875   9906492
oscarl        505    oscarl
>>> b
         assignment:5 studentid
2010453            70   2010453
2112086            82   2112086
2222220            76   2222220
2346979           NaN   2346979
2414996            88   2414996
2438767            50   2438767
2451924           100   2451924
2515531            50   2515531
2536751           100   2536751
2538371            94   2538371
2549050           100   2549050
2551532           100   2551532
2610206            50   2610206
2624429           100   2624429
2630017           NaN   2630017
2634405           100   2634405
2641370           100   2641370
2644284           100   2644284
2712372           100   2712372
2714617            69   2714617
2718450           100   2718450
2739711           100   2739711
9906492           100   9906492
>>> pd.merge(a, b, left_on="studentid", right_on="studentid", how="inner")
    mod:user studentid  assignment:5
0    3891583   2010453            70
1     890910   2112086            82
2     201611   2222220            76
3       7084   2346979           NaN
4    1817436   2414996            88
5     884012   2438767            50
6   20815145   2451924           100
7    2115829   2515531            50
8     494565   2536751           100
9     315295   2549050           100
10    544968   2551532           100
11   1257038   2610206            50
12    939670   2624429           100
13         6   2630017           NaN
14   1093092   2634405           100
15   2038012   2641370           100
16    658743   2644284           100
17      9468   2712372           100
18   1231577   2714617            69
19   3907345   2718450           100
20    396876   2739711           100
21    920875   9906492           100
>>> a.join(b, on="studentid", rsuffix="r", how="inner")
Empty DataFrame
Columns: [mod:user, studentid, assignment:5, studentidr]
Index: []
>>> 

有时候为了让事情变得更加奇怪,在另外一组数据帧上,merge(df1, df2)不能运行,但是 df1.join(df2) 可以正常工作。

>>> a
         mod:user  studentid
2115728   1177712    2115728
2341322    142805    2341322
2447383   1642046    2447383
2510156       141    2510156
2512053    570889    2512053
2527456  12262284    2527456
2529917  11826381    2529917
2533588    183665    2533588
2535922    107131    2535922
2535991    542259    2535991
2543095  11614678    2543095
2548984       225    2548984
2549565   2059072    2549565
2632847  25408938    2632847
2634371    129605    2634371
2714666    755975    2714666
8307654     74576    8307654
>>> b
         assignment:5 studentid
2115728         86.67   2115728
2341322         86.67   2341322
2447383         80.00   2447383
2512053         93.33   2512053
2527456         93.33   2527456
2529917         86.67   2529917
2533588         86.67   2533588
2535922         86.67   2535922
2535991         86.67   2535991
2543095        100.00   2543095
2548984        100.00   2548984
2549565         86.67   2549565
2632847        100.00   2632847
2634371         73.33   2634371
2714666         80.00   2714666
8307654         86.67   8307654
>>> pd.merge(a, b, left_on="studentid", right_on="studentid", how="inner")
Empty DataFrame
Columns: [mod:user, studentid, assignment:5]
Index: []
>>> a.join(b, on="studentid", rsuffix="r", how="inner")
         mod:user  studentid  assignment:5 studentidr
2115728   1177712    2115728         86.67    2115728
2341322    142805    2341322         86.67    2341322
2447383   1642046    2447383         80.00    2447383
2512053    570889    2512053         93.33    2512053
2527456  12262284    2527456         93.33    2527456
2529917  11826381    2529917         86.67    2529917
2533588    183665    2533588         86.67    2533588
2535922    107131    2535922         86.67    2535922
2535991    542259    2535991         86.67    2535991
2543095  11614678    2543095        100.00    2543095
2548984       225    2548984        100.00    2548984
2549565   2059072    2549565         86.67    2549565
2632847  25408938    2632847        100.00    2632847
2634371    129605    2634371         73.33    2634371
2714666    755975    2714666         80.00    2714666
8307654     74576    8307654         86.67    8307654
>>> 

我真的不知道发生了什么,也不知道该使用哪个函数


你使用的是哪个pandas版本?在0.12版本中似乎可以正常工作。 - Andy Hayden
2个回答

3

谢谢!我找到了问题所在...它是在读入数据框时的自动数据转换。

正如你所看到的,在第一个示例中,有人将字符串“oscarl”作为学生ID,并导致整个列被视为字符串,而在第二个示例中没有这样的记录,因此被转换为int。

通过运行建议的解决方案,我发现了这个问题,这使我朝着正确的方向前进。

pd.concat([a, b], axis=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
...
...
Exception: ('Cannot have duplicate column names split across dtypes', 'occurred at index assignment:5')

1
有趣的是,我在pandas 0.12上无法重现您的合并的空数据框,似乎运行良好。我建议在这里进行连接(如果它们被索引,则效果更好,但没有重复的studentid列)。好处是这适用于多个数据帧/系列。
In [11]: df = pd.concat([a, b], axis=1)

In [12]: del df['studentid']  # cleaner if you don't have these as cols in the first place

In [13]: df
Out[13]:
         mod:user  assignment:5
2115728   1177712         86.67
2341322    142805         86.67
2447383   1642046         80.00
2510156       141           NaN
2512053    570889         93.33
2527456  12262284         93.33
2529917  11826381         86.67
2533588    183665         86.67
2535922    107131         86.67
2535991    542259         86.67
2543095  11614678        100.00
2548984       225        100.00
2549565   2059072         86.67
2632847  25408938        100.00
2634371    129605         73.33
2714666    755975         80.00
8307654     74576         86.67

您可以使用join='inner'参数来排除NaN值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接