【问题标题】:Fill columns values according to a match between the columns names and the items of a list in another column根据列名与另一列中列表项之间的匹配填充列值
【发布时间】:2020-11-26 15:09:07
【问题描述】:

我想根据列名或其子字符串与同一行但另一列中的列表项之间的匹配,用“是”或“否”填充列值。有没有办法使用熊猫来实现这一点?

Out[5]:
  Insurance:retailers Insurance:buyers Insurance:sales                Types
0                                                        [retailers, sales]
1                                                                   [sales]
2                                                       [retailers, buyers]

我正在尝试达到以下结果:

Out[7]:
  Insurance:retailers Insurance:buyers Insurance:sales                Types
0                 yes               no             yes   [retailers, sales]
1                  no               no             yes              [sales]
2                 yes              yes              no  [retailers, buyers]

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: python-3.x pandas dataframe data-science series


    【解决方案1】:

    对 pandas 了解不多,但据我了解,panda 的 DataFrames 可以从 python 字典构造,反之亦然。

    所以这应该为您指明正确的方向

    data={'Insurance:retailers':['No','No','No'],
      'Insurance:buyers':['No','No','No'],
      'Insurance:sales':['No','No','No'],
      'Types':[['retailers', 'sales'],['sales'],['retailers', 'buyers']]}
    
    for idx, entry in enumerate(data['Types']):
        for key, value in data.items():
            if any(element in key for element in entry):
                data[key][idx]='yes'
                
    print(data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      • 2020-02-17
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      相关资源
      最近更新 更多