如何在 pandas 中创建一个新列,其中包含与先前特定值的索引差异?

How to create a new column in pandas, containing index difference to the previous specific value?

具有以下数据框:

df = pd.DataFrame(np.ones(10).reshape(10,1), columns=['A'])
df.ix[2]['A'] = 0
df.ix[6]['A'] = 0

   A
0  1
1  1
2  0
3  1
4  1
5  1
6  0
7  1
8  1
9  1

我正在尝试添加一个新列 B,它将在第一个“0”事件之前包含在列 A 中出现的多次“1”。预期输出应该是这样的:

   A  B
0  1  0
1  1  2
2  0  0
3  1  0
4  1  0
5  1  3
6  0  0
7  1  0
8  1  0
9  1  3

有什么有效的矢量化方法可以做到这一点?

您可以使用:

a = df.A.groupby((df.A != df.A.shift()).cumsum()).cumcount() + 1
print (a)
0    1
1    2
2    1
3    1
4    2
5    3
6    1
7    1
8    2
9    3
dtype: int64

b = ((~df.A.astype(bool)).shift(-1).fillna(df.A.iat[-1].astype(bool)))
print (b)
0    False
1     True
2    False
3    False
4    False
5     True
6    False
7    False
8    False
9     True
Name: A, dtype: bool
df['B'] = ( a * b )
print (df)
     A  B
0  1.0  0
1  1.0  2
2  0.0  0
3  1.0  0
4  1.0  0
5  1.0  3
6  0.0  0
7  1.0  0
8  1.0  0
9  1.0  3

解释:

#difference with shifted A
df['C'] = df.A != df.A.shift()
#cumulative sum
df['D'] = (df.A != df.A.shift()).cumsum()
#cumulative count each group
df['a'] = df.A.groupby((df.A != df.A.shift()).cumsum()).cumcount() + 1

#invert and convert  to boolean
df['F'] = ~df.A.astype(bool)
#shift
df['G'] = (~df.A.astype(bool)).shift(-1)
#fill last nan
df['b'] = (~df.A.astype(bool)).shift(-1).fillna(df.A.iat[-1].astype(bool))

print (df)
     A  B      C  D  a      F      G      b
0  1.0  0   True  1  1  False  False  False
1  1.0  2  False  1  2  False   True   True
2  0.0  0   True  2  1   True  False  False
3  1.0  0   True  3  1  False  False  False
4  1.0  0  False  3  2  False  False  False
5  1.0  3  False  3  3  False   True   True
6  0.0  0   True  4  1   True  False  False
7  1.0  0   True  5  1  False  False  False
8  1.0  0  False  5  2  False  False  False
9  1.0  3  False  5  3  False    NaN   True

最后一个NaN有问题。所以我通过 df.A.iat[-1] 检查列 A 的最后一个值并转换为 boolean。因此,如果它是 0,输出是 False,最后是 0,或者如果是 1,输出是 True,然后使用 [=22= 的最后一个值].