2017-05-05 175 views
1

我有一個數據幀(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 

我感謝提供任何幫助:)。謝謝你這麼多

回答

1

試試這個:

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 
+0

MaxU,首先感謝您的回覆。對於第一個選項,它給了我InvalidIndexError:Reindexing只對唯一賦值的Index對象有效,對於第二個選項,它給了我TypeError:'numpy.int64'對象不可調用。我很抱歉:) –

+0

@Araceraceae,'type(frame_concat)'和'frame_concat.dtypes; doc2.dtypes' – MaxU

+0

type(frame_concat):pandas.core.series.Series;類型(frame_concat.dtypes):numpy.dtype和type(doc2.dtypes):pandas.core.series.Series。謝謝你的時間 –

0

什麼簡單?

doc2['Frequency'] = frame_concat