【发布时间】:2021-05-21 13:34:17
【问题描述】:
我在 pandas DataFrame 中有两列,都包含很多空值。 B 列中的某些值部分存在于 A 列的一个字段(或多个字段)中。我想检查 B 的此值是否存在于 A 中,如果存在,则分隔此值并添加为A 列中的新行
例子:
Column A | Column B
black bear | null
black box | null
red fox | null
red fire | null
green tree | null
null | red
null | yellow
null | black
null | red
null | green
我想要以下内容:
Column A
black
bear
box
red
fire
fox
yellow
green
有人对如何获得此结果有任何提示吗?我曾尝试使用正则表达式 (re.match),但我正在努力解决我没有固定模式而是变量(即 B 列中的任何值)的事实这是我的努力:
import re
list_A= df['Column A'].values.tolist()
list_B= df['Column B'].values.tolist()
for i in list_A:
for j in list_B:
if i != None:
if re.match('{j}.+', i) :
...
注意:列长度超过 2500 行。
【问题讨论】: