[英]Pandas check for row equivalence
我有一個包含三列的DataFrame,我想計算上一行中還包含三個值中的多少個。 值是字符串。
原始DF:
Date num1 num2 num3
Y1 x y z
Y2 b x a
Y3 x c c
Y4 c x d
Y5 x c d
所需的輸出:
Date num1
Y1 -
Y2 1 <- since only x in previous row
Y3 1 <- since only x in previous
Y4 2 <- since both x and c in previous
Y5 3 <- since all three in previous row
有什么想法嗎?
通常,在比較要使用shift方法的行時
[90]:
rel = df.set_index('Date')
shifted = rel.shift()
rel.apply(lambda x:x.isin(shifted.loc[x.name]).sum(),axis=1)
Out[90]:
Date
Y1 0
Y2 1
Y3 1
Y4 2
Y5 3
dtype: int64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.