如何根据一列与另一列的交集来分隔列的字符?

发布于 2025-01-10 05:20:47 字数 316 浏览 3 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(2

裸钻 2025-01-17 05:20:47

用空字符串替换常见字符:

f_diff = lambda x: x['nums'].replace(x['number'], '')
df['extra'] = df[['number', 'nums']].astype(str).apply(f_diff, axis=1)
print(df)

# Output
   number    nums extra
0    2830   2830S     S
1    8457   8457M     M
2    9234  923442    42

更新

如果 number 值始终是 nums 列的第一个字符,您可以使用更简单的函数:

f_diff2 = lambda x: x['nums'][len(x['number']):]
df['extra'] = df[['number', 'nums']].astype(str).apply(f_diff2, axis=1)
print(df)

# Output
# Output
   number    nums extra
0    2830   2830S     S
1    8457   8457M     M
2    9234  923442    42

Replace common characters by empty string:

f_diff = lambda x: x['nums'].replace(x['number'], '')
df['extra'] = df[['number', 'nums']].astype(str).apply(f_diff, axis=1)
print(df)

# Output
   number    nums extra
0    2830   2830S     S
1    8457   8457M     M
2    9234  923442    42

Update

If number values are always the first characters of nums column, you can use a simpler function:

f_diff2 = lambda x: x['nums'][len(x['number']):]
df['extra'] = df[['number', 'nums']].astype(str).apply(f_diff2, axis=1)
print(df)

# Output
# Output
   number    nums extra
0    2830   2830S     S
1    8457   8457M     M
2    9234  923442    42
久光 2025-01-17 05:20:47

我会删除字符串的前缀。为此,您可以使用方法 apply() 在每一行上应用以下函数:

def remove_prefix(text, prefix):
    if text.startswith(prefix):
            return text[len(prefix):]
    return text

df['nums'] = df.apply(lambda x: remove_prefix(x['nums'], str(x['number'])), axis=1)
df

输出:

    number  nums
0   2830    S
1   8457    M
2   9234    42

如果您的 python 版本 >= 3.9,则只需要这个:

df['nums'] = df.apply(lambda x: x['nums'].removeprefix(x['number']), axis=1)

I would delete the prefix of the string. For this you can the method apply() to apply following function on each row:

def remove_prefix(text, prefix):
    if text.startswith(prefix):
            return text[len(prefix):]
    return text

df['nums'] = df.apply(lambda x: remove_prefix(x['nums'], str(x['number'])), axis=1)
df

Output:

    number  nums
0   2830    S
1   8457    M
2   9234    42

If you have python version >= 3.9 you only need this:

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