【问题标题】:Finding letters in a column and extracting a row containing a sepecific letter在列中查找字母并提取包含特定字母的行
【发布时间】:2021-12-20 23:21:33
【问题描述】:

我正在尝试使用 Pandas 编辑数据。

  1. 我想通过在列中搜索字母来查找“File1”中的字母。
  2. 我想在“文件 2”中搜索“文件 1”中找到的字母时找到包含这些字母的行。

在这种情况下我有什么办法吗?

【问题讨论】:

  • 文件1和文件2是dataframe格式吗?
  • 如果您在问题的文本中包含文件 1 和文件 2,您将获得更好的帮助
  • 是的!它们都是数据帧格式。

标签: python pandas string dataframe contains


【解决方案1】:

试试这个:

df1 = pd.DataFrame({'file1' : ['JVD', 'WFX', 'DGC', 'HHD', 'WEV', 'IUV',
                               'MDE']})

df2 = pd.DataFrame({'file2' : ['123JVD42', '123WFX3', '234WEV', '231sD2']})

searchfor = list(df1['file1'])
df2[df2['file2'].str.contains('|'.join(searchfor))]

【讨论】:

    【解决方案2】:

    你可以的

    df2['new'] = df2['col'].str.findall('|'.join(df1['col'].tolist()))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 2010-12-27
      • 2022-11-17
      • 2017-06-13
      • 2021-06-17
      相关资源
      最近更新 更多