Pandas中的数据帧合并

时间:2013-08-24 14:32:51

标签: python pandas

出于某种原因,我无法使此合并正常工作。

此数据框(rspars)有2,000多行...

    rsparid  f1mult  f2mult  f3mult
 0        1   0.318   0.636   0.810
 1        2   0.348   0.703   0.893
 2        3   0.384   0.777   0.000
 3        4   0.296   0.590   0.911
 4        5   0.231   0.458   0.690
 5        6   0.275   0.546   0.839
 6        7   0.248   0.486   0.731
 7        8   0.430   0.873   0.000
 8        9   0.221   0.438   0.655
 9       11   0.204   0.399   0.593

尝试根据{Data}框架的rsparid列将上述内容加入到表中时

            line_track  line_race rsparid
 line_date                               
 2013-03-23         TP         10    1400
 2013-02-23         GP          7     634
 2013-01-01         GP          7    1508
 2012-11-11        AQU          5      96
 2012-10-11        BEL          2     161

使用此...

 df = pd.merge(datalines, rspars, how='left', on='rsparid')

我得到了空白..

   line_track  line_race rsparid  f1mult  f2mult  f3mult
 0         TP         10    1400     NaN     NaN     NaN
 1         TP         10    1400     NaN     NaN     NaN
 2         TP         10    1400     NaN     NaN     NaN
 3         GP          7     634     NaN     NaN     NaN
 4         GP         10     634     NaN     NaN     NaN

注意,“datalines”列可以比rspar多数千行,因此左连接。我一定做错了什么?

我也是这样试过的......

 df = datalines.merge(rspars, how='left', on='rsparid')

示例#2

我把数据放到了几行......

rspars:

    rsparid  f1mult  f2mult  f3mult
 0     1400   0.216   0.435   0.656

datalines:

   rsparid
 0    1400
 1     634
 2    1508
 3      96
 4     161
 5    1011
 6    1007
 7     518
 8    1955
 9     678

...合并

 datalines.merge(rspars, how='left', on='rsparid')

...输出

   rsparid  f1mult  f2mult  f3mult
 0    1400     NaN     NaN     NaN
 1     634     NaN     NaN     NaN
 2    1508     NaN     NaN     NaN
 3      96     NaN     NaN     NaN
 4     161     NaN     NaN     NaN
 5    1011     NaN     NaN     NaN
 6    1007     NaN     NaN     NaN
 7     518     NaN     NaN     NaN
 8    1955     NaN     NaN     NaN
 9     678     NaN     NaN     NaN

1 个答案:

答案 0 :(得分:4)

NaN表示他们在rsparid中没有共同的值。在合并repr

时可能看起来相同的内容时,这可能会非常棘手

具有字符串(整数)或整数的小DataFrames的repr看起来相同,并且当帧很小时不打印dtype信息。您可以通过调用DataFrame.info()方法获取小型框架的此信息(以及更多信息),如下所示:df.info()。这将为您提供DataFrame中的内容以及其列的dtype内容的完整摘要:

In [205]: datalines_int = DataFrame({'rsparid':[1400,634,1508,96,161,1011,1007,518,1955,678]})

In [206]: datalines_str = DataFrame({'rsparid':map(str,[1400,634,1508,96,161,1011,1007,518,1955,678])})

In [207]: datalines_int
Out[207]:
   rsparid
0     1400
1      634
2     1508
3       96
4      161
5     1011
6     1007
7      518
8     1955
9      678

In [208]: datalines_str
Out[208]:
  rsparid
0    1400
1     634
2    1508
3      96
4     161
5    1011
6    1007
7     518
8    1955
9     678

In [209]: datalines_int.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 0 to 9
Data columns (total 1 columns):
rsparid    10  non-null values
dtypes: int64(1)

In [210]: datalines_str.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 0 to 9
Data columns (total 1 columns):
rsparid    10  non-null values
dtypes: object(1)

注意:您会在repr处注意到轻微的差异,这很可能是因为数字DataFrame的填充。重点是,除非他们专门寻找差异,否则没有人真正能够以交互方式看到它。