计数从另一列中的一列中的字符串出现

发布于 2025-02-03 19:09:11 字数 795 浏览 1 评论 0原文

我有一个看起来像这样的DF:

  Policy Letter             Password Lower Upper
0    4-5      l               rllllj     4     5
1   4-10      s  ssskssphrlpscsxrfsr     4    10
2  14-18      p  ppppppppppppppppppp    14    18
3    1-6      z       zzlzvmqbzzclrz     1     6
4    4-5      j          jhjjhxhjkxj     4     5

我想计算“字母”列中的字母在密码中出现多少次 从每行的“密码”列。

换句话说,第一行密码中有多少个L'S(4)。 第二行密码中有多少个(8)。

等等。

如果我这样做:

df['Count'] = df['Password'].str.count('s')

它运行正确,但仅计入列中的每个密码中的S。

当我尝试此操作时:

df['Count'] = df['Password'].str.count(df['Letter'])

它会引发错误:

TypeError: 'Series' objects are mutable, thus they cannot be hashed

我不知道如何(如果可能)获得str.Count()检查每行的不同值。

I have a df that looks like this:

  Policy Letter             Password Lower Upper
0    4-5      l               rllllj     4     5
1   4-10      s  ssskssphrlpscsxrfsr     4    10
2  14-18      p  ppppppppppppppppppp    14    18
3    1-6      z       zzlzvmqbzzclrz     1     6
4    4-5      j          jhjjhxhjkxj     4     5

I want to count how many times the letter in the 'Letter' column appears in the password
from the 'Password' column on for each row.

In other words, how many l's are in the password for the first row (4).
How many s's in the password for the second row (8).

And so on.

If I do this:

df['Count'] = df['Password'].str.count('s')

It runs correctly but it only counts s's in every password in the column.

When I try this:

df['Count'] = df['Password'].str.count(df['Letter'])

it throws an error:

TypeError: 'Series' objects are mutable, thus they cannot be hashed

I do not know how (if possible) to get str.count() to check a different value for each row.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2025-02-10 19:09:11

您可以在每行(例如循环)上应用自定义功能:

df['Count'] = df.apply(lambda x: x['Password'].count(x['Letter']), axis=1)
print(df)

# Output
  Policy Letter             Password  Lower  Upper  Count
0    4-5      l               rllllj      4      5      4
1   4-10      s  ssskssphrlpscsxrfsr      4     10      8
2  14-18      p  ppppppppppppppppppp     14     18     19
3    1-6      z       zzlzvmqbzzclrz      1      6      6
4    4-5      j          jhjjhxhjkxj      4      5      5

具有理解力:

df['Count'] = [x.Password.count(x.Letter) for x in df.itertuples()]
# df['Count'] = [x[3].count(x[2]) for x in df.itertuples()]

You can apply a custom function on each row (like a loop):

df['Count'] = df.apply(lambda x: x['Password'].count(x['Letter']), axis=1)
print(df)

# Output
  Policy Letter             Password  Lower  Upper  Count
0    4-5      l               rllllj      4      5      4
1   4-10      s  ssskssphrlpscsxrfsr      4     10      8
2  14-18      p  ppppppppppppppppppp     14     18     19
3    1-6      z       zzlzvmqbzzclrz      1      6      6
4    4-5      j          jhjjhxhjkxj      4      5      5

With a comprehension:

df['Count'] = [x.Password.count(x.Letter) for x in df.itertuples()]
# df['Count'] = [x[3].count(x[2]) for x in df.itertuples()]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文