在不使用if语句的情况下,根据值的变化时间重写数据框中的列单元格值



我有一个有错误值的列,因为它应该计数周期,但是设备中的数据在50之后重置计数,所以我留下了示例[1,1,1,1,1,2,2,2,3,3,3,3,…,50,50,50,1,1,1,2,2,2,2,3,3,3,…,50,50,50,1,1,1,2,2,2,2,3,3,3,…,50,50,.....,50]我的解决方案是,我甚至不能使它工作:(为了简单起见,我从10个周期重置数据

data = {'Cyc-Count':[1,1,2,2,2,3,4,5,6,7,7,7,8,9,10,1,1,1,2,3,3,3,3,
4,4,5,6,6,6,7,8,8,8,8,9,10]}
df = pd.DataFrame(data)
x=0
count=0
old_value=df.at[x,'Cyc-Count']
for x in range(x,len(df)-1):
if df.at[x,'Cyc-Count']==df.at[x+1,'Cyc-Count']:
old_value=df.at[x+1,'Cyc-Count']
df.at[x+1,'Cyc-Count']=count

else:
old_value=df.at[x+1,'Cyc-Count']
count+=1
df.at[x+1,'Cyc-Count']=count

我需要解决这个问题,但最好不使用if语句上面示例的期望输出应该是

data = {'Cyc-Count':[1,1,2,2,2,3,4,5,6,7,7,7,8,9,10,11,11,11,12,13,13,13,13,
14,14,15,16,16,16,17,18,18,18,18,19,20]}

hint"我的方法有一个很大的问题是,最后一个索引值将很难改变,因为当它与它的索引+1>它甚至不存在

如果您想在计数器减少时继续计数。

你可以使用向量代码:

s = df['Cyc-Count'].shift()
df['Cyc-Count2'] = (df['Cyc-Count']
+ s.where(s.gt(df['Cyc-Count']))
.fillna(0, downcast='infer')
.cumsum()
)

或者,就地修改列:

s = df['Cyc-Count'].shift()
df['Cyc-Count'] +=  (s.where(s.gt(df['Cyc-Count']))
.fillna(0, downcast='infer').cumsum()
)

输出:

Cyc-Count  Cyc-Count2
0           1           1
1           1           1
2           1           1
3           1           1
4           2           2
5           2           2
6           2           2
7           3           3
8           3           3
9           3           3
10          3           3
11          4           4
12          5           5
13          5           5
14          5           5
15          1           6
16          1           6
17          1           6
18          2           7
19          2           7
20          2           7
21          2           7
22          3           8
23          3           8
24          3           8
25          4           9
26          5          10
27          5          10
28          1          11
29          2          12
30          2          12
31          3          13
32          4          14
33          5          15
34          5          15

输入:使用

l = [1,1,1,1,2,2,2,3,3,3,3,4,5,5,5,1,1,1,2,2,2,2,3,3,3,4,5,5,1,2,2,3,4,5,5]
df = pd.DataFrame({'Cyc-Count': l})

您可以使用df.loc通过标签或布尔数组访问一组行和列。

语法:df。Loc [df['列名']condition, '列名或新列名']= '如果条件满足值'

例如:

import pandas as pd
numbers = {'set_of_numbers': [1,2,3,4,5,6,7,8,9,10,0,0]}
df = pd.DataFrame(numbers,columns=['set_of_numbers'])
print (df)
df.loc[df['set_of_numbers'] == 0, 'set_of_numbers'] = 999
df.loc[df['set_of_numbers'] == 5, 'set_of_numbers'] = 555
print (df)

:"set_of_numbers":[1,2,3,4,5,6,7,8,9,10,0,0)

后:"set_of_numbers":4555年1、2、3日,6,7,8,9,10999999]

最新更新