遍历列值并分配新值(熊猫)

时间:2018-11-21 17:13:26

标签: python pandas

我的pandas数据框位于一列中,如下所示(已删除列标题,它是“ test”):

  

1、1、1、2、2、2、3、3、3,... 119、119、119、120、120、120、1、1、1、2、2、2、3, 3、3,... 119、119、119、120、120、120等

     

列表回收到350000行。我想做的是在列表中的项目达到120,然后重置为1之后,我想按如下所示继续列表:

     

1、1、1、2、2、2、3、3、3,... 119、119、119、120、120、120、121、121、121、122、122、122、122、123, 123、123,... 239、239、239、240、240、240等

我在实现我想做的事情时遇到了麻烦。代码(不起作用,但我认为逻辑很接近)如下:

enter code here

count = 0

for i in c2['test']:

    if i[i-1] == 120 and i == 1: #value previously in column and current value in column
         count += 1 # set multipler to 1 to start, and then as values loop, the counter will get larger
         it = i + (120 * count) # create new value
         c2.set_value(i,'test',it) # set new value; in first instance, this it variable should be 121

    if count >= 1:
         iti = i + (120 * count) # create new value
         c2.set_value(i,'test',iti)# set new value; in second instance, this iti variable should be 122 

谢谢您的帮助!

更新

当我将@RafaelC代码应用于我的数据集时:https://www.dropbox.com/s/h17ssdu7lgwydd9/compiled_training_data.csv?dl=0由于某种原因提供的代码不起作用。

dataset_parent_folder = "compiled_training_data.csv"
compiled_data = pd.read_csv(dataset_parent_folder, header=0)
pd.DataFrame(compiled_data).reset_index()
compiled_data.columns = ['test']
MAX = (120*3)+1 #I multiplied by 3 as 3 datasets are merged, and added 1 to get to a target of 360
D = 1 - (MAX-1)
n = compiled_data.test.diff().eq(D).cumsum()*(MAX-1) + compiled_data.test
1180838 rows × 1 columns

应用重新索引后的输出:

test
    1   1
    2   1
    3   1
    4   1
    5   1
    6   1
    7   1
    8   1
    9   1
    10  1
    11  1
    12  1
    13  1
    14  1
    15  1
    16  1
    17  1
    18  1
    19  1
    20  1
    21  1
    22  1
    23  1
    24  1
    25  1
    26  1
    27  1
    28  1
    29  1
    ... ...
    1180808 120
    1180809 120
    1180810 120
    1180811 120
    1180812 120
    1180813 120
    1180814 120
    1180815 120
    1180816 120
    1180817 120
    1180818 120
    1180819 120
    1180820 120
    1180821 120
    1180822 120
    1180823 120
    1180824 120
    1180825 120
    1180826 120
    1180827 120
    1180828 120
    1180829 120
    1180830 120
    1180831 120
    1180832 120
    1180833 120
    1180834 120
    1180835 120
    1180836 120
    1180837 120
    1180838 rows × 1 columns

应用@RafaelC的答案后的输出

    test
0   1
1   1
2   1
3   1
4   1
5   1
6   1
7   1
8   1
9   1
10  1
11  1
12  1
13  1
14  1
15  1
16  1
17  1
18  1
19  1
20  1
21  1
22  1
23  1
24  1
25  1
26  1
27  1
28  1
29  1
... ...
1180808 120
1180809 120
1180810 120
1180811 120
1180812 120
1180813 120
1180814 120
1180815 120
1180816 120
1180817 120
1180818 120
1180819 120
1180820 120
1180821 120
1180822 120
1180823 120
1180824 120
1180825 120
1180826 120
1180827 120
1180828 120
1180829 120
1180830 120
1180831 120
1180832 120
1180833 120
1180834 120
1180835 120
1180836 120
1180837 120
1180838 rows × 1 columns

不确定列中是否存在类型问题,我将列转换为int64,但仍然无法解决问题。

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

可复制的示例:

MAX = 4
df4 = pd.DataFrame({'col': np.repeat(np.arange(1,MAX), 3).tolist()*3})

然后,使用diff()cumsum()和一些简单的算术

D = 1 - (MAX-1)
n = df4.col.diff().eq(D).cumsum()*(MAX-1) + df4.col

可重现的示例是

0     1
1     1
2     1
3     2
4     2
5     2
6     3
7     3
8     3
9     1
10    1
11    1
12    2
13    2
14    2
15    3
16    3
17    3
18    1
19    1
20    1
21    2
22    2
23    2
24    3
25    3
26    3
Name: col, dtype: int64

输出为

0     1
1     1
2     1
3     2
4     2
5     2
6     3
7     3
8     3
9     4
10    4
11    4
12    5
13    5
14    5
15    6
16    6
17    6
18    7
19    7
20    7
21    8
22    8
23    8
24    9
25    9
26    9
Name: col, dtype: int64