python - 迭代列值并分配新值(熊猫)
问题描述
我的熊猫数据框在一列中,如下所示(列标题已删除,它是“测试”):
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, 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,但仍然没有解决问题。
谢谢您的帮助!
解决方案
可重现的例子:
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
推荐阅读
- symfony - 在 symfony4 表单中对 CollectionType 使用 query_builder?
- python - 如何加载和使用预置的 PyTorch InceptionV3 模型对图像进行分类
- java - 我有一个文件 abc.txt 但实际上它是 abc.java(意味着文件中有 java 代码),那么如何通过其内容获得正确的扩展名?
- php - 如何正确地将mysql中的数据放入其中
- ?
- python - python selenium firefox - add_extension 不工作
- android - 如何在 Android 上获取 InputMethod 的窗口
- typescript - 如何在一个组件(html)中实现onclick以及从Angular 6中的另一个组件调用的功能
- testing - ETL 测试中的关键错误是什么,我们如何找到它?
- java - 如何在 Java 中删除 JSON 多余的行?
- swift - 缩小全屏图像的大小