在python熊猫中映射

时间:2020-04-29 18:01:06

标签: python pandas

我有2个数据框:

{'base_conditions': [(True, True),
                     (True, False),
                     (False, True),
                     (False, False)],
 'bases': ['A', 'B'],
 'ints': True,
 'p': re.compile('(?<!\\w)(A|B)(?!\\w)'),
 'parens': nested () expression,
 'phrases': ['A xor B'],
 'to_match': {W:(ABCD...) | "A" | "B" | "not" | "-" | "~" | "or" | "nor" | "xor" | "and" | "nand" | "=>" | "implies" | "=" | "!="}}

是否仍然要检查df1中是否存在df2.Numbers,如果是,则将相关的df1.names提取到单独的数据框中? 预期输出:

NcG.base_conditions

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

IIUC,使用set_indexstack,然后合并到df2

您还应该考虑Quang的建议。

s = df1.set_index('names').stack().reset_index(0).rename({0 : 'Numbers'},axis=1)


df3 = pd.merge(df2,s,on='Numbers',how='left')

print(df3)

     idx  Numbers    names
0  54174   42.775     efgh
1  54175   43.900  abcdefg
2  54176   35.550      NaN
3  54177   35.550      NaN
4  54178   43.900  abcdefg