dimanche 12 juin 2016

Separating elements of a Pandas DataFrame in Python


I have a pandas DataFrame that looks like the following:

    Time  Measurement
0      0            1
1      1            2
2      2            3
3      3            4
4      4            5
5      0            2
6      1            3
7      2            4
8      3            5
9      4            6
10     0            3
11     1            4
12     2            5
13     3            6
14     4            7
15     0            1
16     1            2
17     2            3
18     3            4
19     4            5
20     0            2
21     1            3
22     2            4
23     3            5
24     4            6
25     0            3
26     1            4
27     2            5
28     3            6
29     4            7

which can be generated with the following code:

import pandas
time=[0,1,2,3,4]
repeat_1_conc_1=[1,2,3,4,5]
repeat_1_conc_2=[2,3,4,5,6]
repeat_1_conc_3=[3,4,5,6,7]
d1=pandas.DataFrame([time,repeat_1_conc_1]).transpose()
d2=pandas.DataFrame([time,repeat_1_conc_2]).transpose()
d3=pandas.DataFrame([time,repeat_1_conc_3]).transpose()
repeat_2_conc_1=[1,2,3,4,5]
repeat_2_conc_2=[2,3,4,5,6]
repeat_2_conc_3=[3,4,5,6,7]
d4=pandas.DataFrame([time,repeat_2_conc_1]).transpose()
d5=pandas.DataFrame([time,repeat_2_conc_2]).transpose()
d6=pandas.DataFrame([time,repeat_2_conc_3]).transpose()
df= pandas.concat([d1,d2,d3,d4,d5,d6]).reset_index()
df.drop('index',axis=1,inplace=True)
df.columns=['Time','Measurement']
print df

If you look at the code, you'll see that I have two experimental repeats in the same DataFrame which should be separated at df.iloc[:15]. Additionally, within each experiment I have 3 sub-experiments that can be thought of like the starting conditions of a dose response, i.e. first sub-experiment starts with 1, second with 2 and third with 3. These should be separated at index intervals of `len(time)', which is 0-4, 5 elements for each experimental repeat. Could somebody please tell me the best way to separate this data into individual time course measurements for each experiment? I'm not exactly sure what the best data structure would be to use but I just need to be able to access each data for each sub experiment for each experimental repeat easily. Perhaps sometime like:

repeat1=
    Time  Measurement
0      0            1
1      1            2
2      2            3
3      3            4
4      4            5


5      0            2
6      1            3
7      2            4
8      3            5
9      4            6


10     0            3
11     1            4
12     2            5
13     3            6
14     4            7

Repeat 2=
      Time  Measurement
15     0            1
16     1            2
17     2            3
18     3            4
19     4            5


20     0            2
21     1            3
22     2            4
23     3            5
24     4            6


25     0            3
26     1            4
27     2            5
28     3            6
29     4            7

Aucun commentaire:

Enregistrer un commentaire