比较熊猫系列中连续行的字符串值

时间:2021-05-22 12:16:37

标签: pandas loops for-loop apply shift

我正在尝试使用用户定义的函数计算熊猫系列连续行中的常见字符串值,并将输出写入新列。我想出了单独的步骤,但是当我把它们放在一起时,我得到了错误的结果。你能告诉我最好的方法吗?我是一个非常初级的 Pythonista!

我的熊猫 df 是:

df = pd.DataFrame({"Code": ['d7e', '8e0d', 'ft1', '176', 'trk', 'tr71']})

我的字符串比较循环是:

x='d7e'
y='8e0d'
s=0
for i in y:
   b=str(i)
      if b not in x:
          s+=0
      else:
          s+=1
print(s)

这些特定字符串的正确结果是 2

注意,当我执行 def func(x,y) 时: s 计数器发生了一些事情并且它没有产生正确的结果。我想每次循环运行时我都需要将其重置为 0。

然后,我使用 df.shift 来指定 y 和 x 在一个系列中的位置:

x = df["Code"]
y = df["Code"].shift(periods=-1, axis=0)

最后,我使用 df.apply() 方法来运行函数:

df["R1SB"] = df.apply(func, axis=0)

我的新列“R1SB”中没有值

我的正确输出是:

    "Code"   "R1SB"
0    d7e      None
1    8e0d     2
2    ft1      0
3    176      1
4    trk      0
5    tr71     2

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

尝试:

df['R1SB'] = df.assign(temp=df.Code.shift(1)).apply(
    lambda x: np.NAN
    if pd.isna(x['temp'])
    else sum(i in str(x['temp']) for i in str(x['Code'])),
    1,
)

输出:

   Code  R1SB
0   d7e   NaN
1  8e0d   2.0
2   ft1   0.0
3   176   1.0
4   trk   0.0
5  tr71   2.0