如何根据一列与另一列的交集来分隔列的字符?
我的 df 有两列,第二列包含另一列的数据+其他字符(字母和/或数字):
values = {
'number': [2830, 8457, 9234],
'nums': ['2830S', '8457M', '923442']
}
df = pd.DataFrame(values, columns=['number', 'nums'])
额外的字符始终位于常见字符之后!如何分离两列之间不常见的字符?我正在寻找一个简单的解决方案,而不是一个循环来检查每个字符。
There are two columns in my df, the second column includes data of the other column+other characters (alphabets and/or numbers):
values = {
'number': [2830, 8457, 9234],
'nums': ['2830S', '8457M', '923442']
}
df = pd.DataFrame(values, columns=['number', 'nums'])
The extra characters are always after the common characters! How can I separate the characters that are not common between the two columns? I am looking for a simple solution, not a loop to check every character.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用空字符串替换常见字符:
更新
如果
number
值始终是nums
列的第一个字符,您可以使用更简单的函数:Replace common characters by empty string:
Update
If
number
values are always the first characters ofnums
column, you can use a simpler function:我会删除字符串的前缀。为此,您可以使用方法
apply()
在每一行上应用以下函数:输出:
如果您的 python 版本 >= 3.9,则只需要这个:
I would delete the prefix of the string. For this you can the method
apply()
to apply following function on each row:Output:
If you have python version >= 3.9 you only need this: