我想创建一个C列(基于B),该列从B中的“ 100”开始计算系列的每个开始。我有以下熊猫数据框:
A B
1 0
2 0
3 100
4 100
5 100
6 0
7 0
8 100
9 100
10 100
11 100
12 0
13 0
14 0
15 100
16 100
我要创建以下C列:
A C
1 0
2 0
3 1
4 1
5 1
6 0
7 0
8 2
9 2
10 2
11 2
12 0
13 0
14 0
15 3
16 3
此C列应该算出每100个系列。
谢谢。
答案 0 :(得分:2)
使用:
df['C'] = (df['B'].shift(-1).eq(100) & df['B'].ne(100)).cumsum() * df['B'].eq(100)
print (df)
A B C
0 1 0 0
1 2 0 0
2 3 100 1
3 4 100 1
4 5 100 1
5 6 0 0
6 7 0 0
7 8 100 2
8 9 100 2
9 10 100 2
10 11 100 2
11 12 0 0
12 13 0 0
13 14 0 0
14 15 100 3
15 16 100 3
详细信息和说明:
Series.shift
比较Series.eq
与==
&
对bitwise AND
的束缚条件,Series.ne
对!=
的Trues束缚,Series.cumsum
作为计数器0
的{{3}}列乘以df = df.assign(shifted = df['B'].shift(-1).eq(100),
chained = df['B'].shift(-1).eq(100) & df['B'].ne(100),
cumsum = (df['B'].shift(-1).eq(100) & df['B'].ne(100)).cumsum(),
eq_100 = df['B'].eq(100),
C = (df['B'].shift(-1).eq(100) & df['B'].ne(100)).cumsum() * df['B'].eq(100))
print (df)
A B shifted chained cumsum eq_100 C
0 1 0 False False 0 False 0
1 2 0 True True 1 False 0
2 3 100 True False 1 True 1
3 4 100 True False 1 True 1
4 5 100 False False 1 True 1
5 6 0 False False 1 False 0
6 7 0 True True 2 False 0
7 8 100 True False 2 True 2
8 9 100 True False 2 True 2
9 10 100 True False 2 True 2
10 11 100 False False 2 True 2
11 12 0 False False 2 False 0
12 13 0 False False 2 False 0
13 14 0 True True 3 False 0
14 15 100 True False 3 True 3
15 16 100 False False 3 True 3
SUSER_NAME()