我有这样的数据。
data = [
['2019-01-01', 'a',0],
['2019-01-02', 'b',0],
['2019-01-03', 'c',0],
['2019-01-04', 'd',0],
['2019-01-05', 'a',0],
['2019-01-05', 'd',0],
['2019-01-06', 'd',0],
['2019-01-07', 'f',0],
['2019-01-08', 'c',0],
['2019-01-08', 'b',0],
['2019-01-08', 'g',0],
['2019-01-08', 'h',0],
['2019-01-09', 'q',0],
['2019-01-09', 'b',0],
['2019-01-09', 'y',0],
['2019-01-10', 'd',0],
['2019-01-11', 'z',0],
['2019-01-11', 'x',0],
['2019-01-11', 'c',0],
['2019-01-12', 'y',0],
['2019-01-13', 'x',0],
['2019-01-13', 'q',0],
['2019-01-14', 't',0],
['2019-01-15', 'i',0]]
df = pd.DataFrame(data, columns = ['Date', 'Column1','Column2'])
Date Column1 Column2
0 2019-01-01 a 0
1 2019-01-02 b 0
2 2019-01-03 c 0
3 2019-01-04 d 0
4 2019-01-05 a 0
5 2019-01-05 d 0
6 2019-01-06 d 0
7 2019-01-07 f 0
8 2019-01-08 c 0
9 2019-01-08 b 0
10 2019-01-08 g 0
11 2019-01-08 h 0
12 2019-01-09 q 0
13 2019-01-09 b 0
14 2019-01-09 y 0
15 2019-01-10 d 0
16 2019-01-11 z 0
17 2019-01-11 x 0
18 2019-01-11 c 0
19 2019-01-12 y 0
20 2019-01-13 x 0
21 2019-01-13 q 0
22 2019-01-14 t 0
23 2019-01-15 i 0
我的目标是查看每个 column1 元素,如果该元素之前存在于 column1 中,则将 column2 的值设为 1。我写了一个这样的代码。
for i in range(0,len(df)):
for j in range(0,i-1):
if df.Column1[i] == df.Column1[j]:
df.Column2[i] = 1
我得到了我想要的结果。
Date Column1 Column2
0 2019-01-01 a 0
1 2019-01-02 b 0
2 2019-01-03 c 0
3 2019-01-04 d 0
4 2019-01-05 a 1
5 2019-01-05 d 1
6 2019-01-06 d 1
7 2019-01-07 f 0
8 2019-01-08 c 1
9 2019-01-08 b 1
10 2019-01-08 g 0
11 2019-01-08 h 0
12 2019-01-09 q 0
13 2019-01-09 b 1
14 2019-01-09 y 0
15 2019-01-10 d 1
16 2019-01-11 z 0
17 2019-01-11 x 0
18 2019-01-11 c 1
19 2019-01-12 y 1
20 2019-01-13 x 1
21 2019-01-13 q 1
22 2019-01-14 t 0
23 2019-01-15 i 0
但是当我在 100000 行数据上运行这段代码时,它运行得很慢。有没有办法在更短的时间内做到这一点,或者对这个问题有不同的解决方案建议吗?
感谢您的回答
最佳答案
您可以在 column1 上执行 groupby 和 cumcount,然后 clip上限为 1:
df['Column2'] = df.groupby("Column1").cumcount().clip(upper=1)
然而,更简洁的是检查 series.duplicated
这里:df['Column2'] = df['Column1'].duplicated().astype(int)
print(df)
Date Column1 Column2
0 2019-01-01 a 0
1 2019-01-02 b 0
2 2019-01-03 c 0
3 2019-01-04 d 0
4 2019-01-05 a 1
5 2019-01-05 d 1
6 2019-01-06 d 1
7 2019-01-07 f 0
8 2019-01-08 c 1
9 2019-01-08 b 1
10 2019-01-08 g 0
11 2019-01-08 h 0
12 2019-01-09 q 0
13 2019-01-09 b 1
14 2019-01-09 y 0
15 2019-01-10 d 1
16 2019-01-11 z 0
17 2019-01-11 x 0
18 2019-01-11 c 1
19 2019-01-12 y 1
20 2019-01-13 x 1
21 2019-01-13 q 1
22 2019-01-14 t 0
23 2019-01-15 i 0
关于python - 为什么代码运行如此之慢以至于我在其中使用了 for 循环。有没有更快的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67624092/