Pandas 数据框根据其他列值将函数应用于列字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36383821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:59:26  来源:igfitidea点击:

Pandas dataframe apply function to column strings based on other column value

pythonpandas

提问by Testy8

I would like to remove all instance of the string in col 'B' from col 'A', like so:

我想从 col 'A' 中删除 col 'B' 中字符串的所有实例,如下所示:

col A                 col B    col C
1999 toyota camry     camry    1999 toyota 
2003 nissan pulsar    pulsar   20013 nissan

How would I do this using pandas? If it was a fixed value (non-dependent on another column), I would use:

我将如何使用Pandas来做到这一点?如果它是一个固定值(不依赖于另一列),我会使用:

df['col C'] = df['col A'].str.replace('value-to-replace','')

采纳答案by Jon Clements

Given a DataFrameof:

给定一个DataFrame

df = pd.DataFrame(
    {
        'A': ['1999 toyota camry', '2003 nissan pulsar'],
        'B': ['camry', 'pulsar']
    }
)

You can df.applyover the row axis and perform the replacement:

您可以df.apply在行轴上执行替换:

df['C'] = df.apply(lambda L: L.A.replace(L.B, ''), axis=1)

This'll give you:

这会给你:

                    A       B             C
0   1999 toyota camry   camry  1999 toyota 
1  2003 nissan pulsar  pulsar  2003 nissan 

回答by Sergey Bushmanov

Suppose you have a dataframe:

假设你有一个数据框:

df

               col A    col B
0   1999 toyota camry   camry
1   2003 nissan pulsar  pulsar

Then you may proceed as follows:

那么您可以按照以下步骤进行:

df['col C'] = [el[0].replace(el[1],'') for el in zip(df['col A'],df['col B'])]
df

                col A   col B         col C
0   1999 toyota camry   camry   1999 toyota
1   2003 nissan pulsar  pulsar  2003 nissan