提取数据帧中的所有行,该数据帧包含另一个数据帧中列的子字符串



假设我有以下两个数据帧:

int = pd.DataFrame({'domain1':['ABC.6','GF53.7','SDC78.12','GGH7T.64'], 'domain2': ['UI76.89','76TH3.2','YU1QW.45','BY76.12']})
domain1         domain2
ABC.6           UI76.89
GF53.7          76TH3.2
SDC78.12        YU1QW.45
GGH7T.64        BY76.12

还有另一个数据帧:

doms = pd.DataFrame({'domains':['GF53','VB96','UI76','GGH7T','BY76','ABC','SDC78']})

domains
GF53
VB96
UI76
GGH7T
BY76
ABC
SDC78

仅当"domain1"one_answers"domain2"列中的值都包含"doms"数据帧中"domain"列的子字符串时,我才希望创建一个新的数据帧,该数据帧将包括"int"数据帧的所有行。例如,在这种情况下,结果应该看起来像:

domain1      domain2
ABC.6        UI76.89
GGH7T.64     BY76.12

只是一些str.contains与联合正则表达式混合:

int[int.domain1.str.contains('|'.join(doms.domains)) &
int.domain2.str.contains('|'.join(doms.domains))]
domain1  domain2
0     ABC.6  UI76.89
3  GGH7T.64  BY76.12

试试这个,DataFrame.stack将行转换为列,然后应用包含来过滤掉DataFrame.unstack后面的值,以返回原始数据。

df[df.stack().str.contains("|".join(doms.domains)).unstack().all(axis=1)]

domain1  domain2
0     ABC.6  UI76.89
3  GGH7T.64  BY76.12

最新更新