计数从另一列中的一列中的字符串出现
我有一个看起来像这样的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在每行(例如循环)上应用自定义功能:
具有理解力:
You can apply a custom function on each row (like a loop):
With a comprehension: