【问题标题】:Filter dataframe in python using pandas based on partial string match基于部分字符串匹配使用pandas在python中过滤数据框
【发布时间】:2020-12-07 21:11:31
【问题描述】:

我的输入数据框是:

list_of_dicts1 = {"Filter":["abc",'def']}

test1 = pd.DataFrame(list_of_dicts1)

list_of_dicts2 = {"C":["a",'z']}

test2 = pd.DataFrame(list_of_dicts2)

期望的输出是

list_of_dicts3 = {"Filter":['abc']}

test3 = pd.DataFrame(list_of_dicts3)

如何使用 pandas 根据 test2 数据帧的“C”列过滤数据帧 test1

【问题讨论】:

    标签: python pandas filter


    【解决方案1】:

    Series.str.containsjoin 一起使用| 用于正则表达式or

    df = test1[test1['Filter'].str.contains('|'.join(test2['C']))]
    print (df)
      Filter
    0    abc
    

    【讨论】:

      猜你喜欢
      • 2019-09-19
      • 2022-01-10
      • 2017-12-23
      • 2017-07-15
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      相关资源
      最近更新 更多