gpt4 book ai didi

python - 每次特定列的字符串内容发生变化时从 0 重新启动索引

转载 作者:太空宇宙 更新时间:2023-11-03 20:35:07 25 4
gpt4 key购买 nike

我想在 pandas 数据框中添加一个索引列,每当特定列的字符串内容发生变化时,索引就会从 0 重新开始。

这是数据框,每次列描述中的 Take #1: 更改为 Take #2: 我想将索引重新启动为 0,当它更改为 Take #3 时也是如此:


Description A B
0 Take #1: (95.448; 7.399; 3.292) in A1 0.78606277704238903 0.235648
1 Take #1: (94.709; 9.619; 3.292) in A1 0.83539319038391102 0.196260
2 Take #1: (92.489; 11.839; 3.292) in A1 0.69859844446182295 0.268558
3 Take #1: (94.955; 12.825; 3.292) in A1 0.83374726772308405 0.211926
4 Take #1: (95.202; 15.538; 3.292) in A1 0.81004440784454301 0.207539
5 Take #1: (93.969; 16.031; 3.292) in A1 0.82159596681594804 0.201048
6 Take #1: (95.448; 18.744; 3.292) in A1 0.81642413139343295 0.209556
7 Take #1: (93.722; 18.991; 3.292) in A1 0.80492436885833696 0.234336
8 Take #1: (94.955; 20.964; 3.292) in A1 0.82342678308486905 0.248848
9 Take #1: (95.695; 22.937; 3.292) in A1 0.800290167331696 0.262177
10 Take #2: (95.448; 7.399; 3.292) in A1 0.77198499441146895 0.219335
11 Take #2: (94.709; 9.619; 3.292) in A1 0.83259344100952104 0.194786
12 Take #2: (92.489; 11.839; 3.292) in A1 0.53725022077560403 0.345984
13 Take #2: (94.955; 12.825; 3.292) in A1 0.84538751840591397 0.199329
14 Take #2: (95.202; 15.538; 3.292) in A1 0.83818721771240201 0.187591
15 Take #2: (93.969; 16.031; 3.292) in A1 0.81123870611190796 0.212484
16 Take #2: (95.448; 18.744; 3.292) in A1 0.79200905561447099 0.221299
17 Take #2: (93.722; 18.991; 3.292) in A1 0.81063342094421398 0.223800
18 Take #2: (94.955; 20.964; 3.292) in A1 0.81234723329544101 0.213572
19 Take #2: (95.695; 22.937; 3.292) in A1 0.82670426368713401 0.234590
20 Take #3: (95.448; 7.399; 3.292) in A1 0.80610674619674705 0.234529
21 Take #3: (94.709; 9.619; 3.292) in A1 0.84471392631530795 0.200126
22 Take #3: (92.489; 11.839; 3.292) in A1 0.55611509084701505 0.344433
23 Take #3: (94.955; 12.825; 3.292) in A1 0.82982766628265403 0.211508
24 Take #3: (95.202; 15.538; 3.292) in A1 0.82371282577514604 0.192534
25 Take #3: (93.969; 16.031; 3.292) in A1 0.81001353263855003 0.211018
26 Take #3: (95.448; 18.744; 3.292) in A1 0.80310589075088501 0.231468
27 Take #3: (93.722; 18.991; 3.292) in A1 0.813673436641693 0.234990
28 Take #3: (94.955; 20.964; 3.292) in A1 0.81725031137466397 0.212430
29 Take #3: (95.695; 22.937; 3.292) in A1 0.80750209093093905 0.230676

最佳答案

这是一种方法:

df = pd.DataFrame({'Description' : ['Take #1 : aezr', 'Take #2 : qsdkf','Take #2 : kfqdskl'],
'A' : [12, 46, 57], 'B' : [13, 46,57]})

# First we find where the takes # change looking in the description field
df['inter'] = [int(x[6]) for x in df['Description'] ]

# Then for each different take we fill the index column using the range function
for x in df['inter'].unique():
mask = df['inter'] == x
df.loc[mask, 'index'] = [x for x in range(len(df[mask]))]

# You can now drop the inter column which is no longer needed :
df.drop(columns='inter', inplace = True)
<小时/>

输出:

         Description   A   B  index
0 Take #1 : aezr 12 13 0.0
1 Take #2 : qsdkf 46 46 0.0
2 Take #2 : kfqdskl 57 57 1.0

关于python - 每次特定列的字符串内容发生变化时从 0 重新启动索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57216806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com