2024-04-18 17:56:30 发布
网友
此框架中的熊猫数据:
df = pd.DataFrame({'a': ['foo_abc', 'bar_def', 'ghi'], 'b': ['foo', 'bar', 'yah']})
a b 0 foo_abc foo 1 bar_def bar 2 ghi yah
我想,可能使用regex,从a列的字符串中删除b列中的字符串以生成
a
b
我怎么能对熊猫这么做呢?在
在列表理解中使用replace和strip:
replace
strip
df['c'] = [a.replace(b, '').strip('_') for a, b in zip(df['a'], df['b'])] print (df) a b c 0 foo_abc foo abc 1 bar_def bar def 2 ghi yah ghi
含有re.sub的溶液:
re.sub
在列表理解中使用
replace
和strip
:含有
^{pr2}$re.sub
的溶液:相关问题 更多 >
编程相关推荐