Pandas - Dataframe与系列值的合并

时间:2017-05-05 12:20:11

标签: python pandas

我有一个Dataframe(doc2)基本上看起来像:

  Index     AgeGroups     Factor Cancer
    0        0_5  wo-statin    Yes
    1       6_10  wo-statin    Yes
    2      11_15  wo-statin    Yes
    3      16_20  wo-statin    Yes
    4      21_25  wo-statin    Yes
    5      26_30  wo-statin    Yes
    6      31_35  wo-statin    Yes
    7      36_40  wo-statin    Yes
    8      41_45  wo-statin    Yes
    9      46_50  wo-statin    Yes
    10     51_55  wo-statin    Yes
    11     56_60  wo-statin    Yes
    12     61_65  wo-statin    Yes
    13     66_70  wo-statin    Yes
    14     71_75  wo-statin    Yes
    15     76_80  wo-statin    Yes
    16     81_85  wo-statin    Yes
    17     86_90  wo-statin    Yes
    18     91_95  wo-statin    Yes
    19    96_100  wo-statin    Yes

和一个系列(frame_concat):

Index         0
0_5           0
6_10          0
11_15         0
16_20         2
21_25         1
26_30         4
31_35         1
36_40         2
41_45         4
46_50         5
51_55         5
56_60         2
61_65         3
66_70         3
71_75         2
76_80         0
81_85         0
86_90         0
91_95         0
96_100        0

现在,基本上我想以这样的方式合并两者(doc2,frame_concat)(来自doc2的AgeGroups =来自frame_concat的索引)并且也许新的列可以称之为包含值的频率

0
0
0
0
2
1
4
1
2
4
5
5
2
3
3
2
0
0
0
0
0

期望像这样得到整体结果:

Index     AgeGroups     Factor Cancer   Frequency
        0        0_5  wo-statin    Yes    0
        1       6_10  wo-statin    Yes    0
        2      11_15  wo-statin    Yes    0
        3      16_20  wo-statin    Yes    2
        4      21_25  wo-statin    Yes    1
        5      26_30  wo-statin    Yes    4
        6      31_35  wo-statin    Yes    1
        7      36_40  wo-statin    Yes    2
        8      41_45  wo-statin    Yes    4
        9      46_50  wo-statin    Yes    5
        10     51_55  wo-statin    Yes    5
        11     56_60  wo-statin    Yes    2
        12     61_65  wo-statin    Yes    3
        13     66_70  wo-statin    Yes    3
        14     71_75  wo-statin    Yes    2
        15     76_80  wo-statin    Yes    0
        16     81_85  wo-statin    Yes    0
        17     86_90  wo-statin    Yes    0
        18     91_95  wo-statin    Yes    0
        19    96_100  wo-statin    Yes    0

我很感谢提供的任何帮助:)。非常感谢你

2 个答案:

答案 0 :(得分:2)

试试这个:

In [170]: doc2.merge(frame_concat.to_frame('Frequency'), left_on='AgeGroups',
                     right_index=True, how='left')
Out[170]:
      AgeGroups     Factor Cancer  Frequency
Index
0           0_5  wo-statin    Yes          0
1          6_10  wo-statin    Yes          0
2         11_15  wo-statin    Yes          0
3         16_20  wo-statin    Yes          2
4         21_25  wo-statin    Yes          1
5         26_30  wo-statin    Yes          4
6         31_35  wo-statin    Yes          1
7         36_40  wo-statin    Yes          2
8         41_45  wo-statin    Yes          4
9         46_50  wo-statin    Yes          5
10        51_55  wo-statin    Yes          5
11        56_60  wo-statin    Yes          2
12        61_65  wo-statin    Yes          3
13        66_70  wo-statin    Yes          3
14        71_75  wo-statin    Yes          2
15        76_80  wo-statin    Yes          0
16        81_85  wo-statin    Yes          0
17        86_90  wo-statin    Yes          0
18        91_95  wo-statin    Yes          0
19       96_100  wo-statin    Yes          0

答案 1 :(得分:0)

简单呢?

doc2['Frequency'] = frame_concat