如何用pandas增加数据帧?

时间:2016-03-09 11:12:17

标签: python pandas dataframe

我必须添加一个列,该列将增加新数据帧上的行数。我制作的解决方案大部分工作但不太好。首先,它耗费时间,其次,前两行包含新列的相同值(来自第2行的值)。这是我的源代码:

# time consuming!
c = 0
for l in range(0, len(data)):
    dic = data.ix[l]
    dic["Sample Type"] = "Trizol"
    if int(dic["BoxPos"]) % 2 == 0:
        for p in range(42, 82):
            dic["Position"] = p
            datatest.loc[c] = dic.values
            c += 1
    else:
        for p in range(1, 41):
            dic["Position"] = p
            if l == 0 and p == 1:
                datatest = pd.DataFrame(dic.to_frame().T)
                c += 1
            else:
                datatest.loc[c] = dic.values
                c += 1

以下是应用代码之前的数据框:

Out[147]: 
       RoomID  RackID  DonorID  BoxPos FreezerID  ShelfID    Box
0  10_Magasin  Rack 1  101122A       1      1532  Shelf 3  box 1
1  10_Magasin  Rack 1  101112A       2      1532  Shelf 3  box 1
2  10_Magasin  Rack 1  102202A       3      1532  Shelf 3  box 2
3  10_Magasin  Rack 1  102162A       4      1532  Shelf 3  box 2
4  10_Magasin  Rack 1  100122A       5      1532  Shelf 3  box 3

新数据框(仅显示两个第一捐赠者):

Out[155]: 
        RoomID  RackID  DonorID BoxPos FreezerID  ShelfID    Box Sample Type  \
0   10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
1   10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
2   10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
3   10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
4   10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
5   10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
6   10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
7   10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
8   10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
9   10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
10  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
11  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
12  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
13  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
14  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
15  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
16  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
17  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
18  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
19  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
20  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
21  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
22  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
23  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
24  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
25  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
26  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
27  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
28  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
29  10_Magasin  Rack 1  101122A      1      1532  Shelf 3  box 1      Trizol   
..         ...     ...      ...    ...       ...      ...    ...         ...   
50  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
51  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
52  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
53  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
54  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
55  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
56  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
57  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
58  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
59  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
60  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
61  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
62  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
63  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
64  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
65  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
66  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
67  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
68  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
69  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
70  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
71  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
72  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
73  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
74  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
75  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
76  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
77  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
78  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   
79  10_Magasin  Rack 1  101112A      2      1532  Shelf 3  box 1      Trizol   

   Position  
0         2  
1         2  
2         3  
3         4  
4         5  
5         6  
6         7  
7         8  
8         9  
9        10  
10       11  
11       12  
12       13  
13       14  
14       15  
15       16  
16       17  
17       18  
18       19  
19       20  
20       21  
21       22  
22       23  
23       24  
24       25  
25       26  
26       27  
27       28  
28       29  
29       30  
..      ...  
50       52  
51       53  
52       54  
53       55  
54       56  
55       57  
56       58  
57       59  
58       60  
59       61  
60       62  
61       63  
62       64  
63       65  
64       66  
65       67  
66       68  
67       69  
68       70  
69       71  
70       72  
71       73  
72       74  
73       75  
74       76  
75       77  
76       78  
77       79  
78       80  
79       81  

我的algortihm有效,但我很确定存在更好的解决方案。我还试图纠正位置列的错误,但我确实管理了什么错误。你们中的任何人都知道如何在代码上增加这一部分吗?

提前致谢。

0 个答案:

没有答案