【问题标题】:I have a code and want a to return single value我有一个代码,想要一个返回单个值
【发布时间】:2019-09-03 12:09:20
【问题描述】:

我有一个数据框,其中有一行名为 items,我有一个名为 topitems 的列表。以下是它的一些前任

Df.head()

Item
Toy
Car, Toy
Buses, Car
Bike
Barbie
Lorri

我的清单是顶级项目

[Toy, Bike, Car]

现在我想要数据框中的另一列称为 Top Item。

我尝试过设置和交集,但它们返回两个匹配的值

对玩具,它返回玩具 d 对玩具和汽车它返回玩具和汽车,但我希望它返回唯一的玩具

dff['topitems'] = dff.items.apply(lambda x: list(set(x).intersection(set(topitems))))

我希望结果如下所示,

Df.head()
Item         | Top item
Toy          |   Toy
Car, Toy     |   Car (note : i don't want the second value even though 
                       it's in my list)
Buses, Car   |   Car
Bike         |   Bike
Barbie       |   Blank
Lorri        |   Blank

【问题讨论】:

  • 也许使用索引 [0] 从列表中获取第一个元素。或者更好的[:1] 在列表为空时跳过错误

标签: python pandas set intersection


【解决方案1】:

您可以使用索引[0] 从列表中获取第一个元素。或者最好使用[:1],当列表为空且没有[0]时不会报错

dff['topitems'] = dff.items.apply(lambda x: list(set(x).intersection(set(topitems)))[:1])

示例代码:

编辑:我删除了intersection() 中的set(),正如评论中建议的@rpanai。

import pandas as pd

dff = pd.DataFrame({'items':[
                        ['Toy'],
                        ['Car', 'Toy'],
                        ['Buses', 'Car'],
                        ['Bike'],
                        ['Barbie'],
                        ['Lorri'],
                    ]})


topitems = ['Toy', 'Bike', 'Car']
dff['topitems'] = dff['items'].apply(lambda x: list(set(x).intersection(topitems))[:1])

print(dff)

【讨论】:

  • 交集后的第二个set 是不必要的。
【解决方案2】:

设置:

df = pd.DataFrame({'Item': {0: 'Toy',
                  1: 'Car, Toy',
                  2: 'Buses, Car',
                  3: 'Bike',
                  4: 'Barbie',
                  5: 'Lorri'}})
topitems = ['Toy', 'Bike', 'Car']

df
    Item
0   Toy
1   Car, Toy
2   Buses, Car
3   Bike
4   Barbie
5   Lorri

解决方案:

您可以先将 Item 拆分为 list 并检查每个元素是否在 topitems 列表中。最后确定第一个出现的项目(如果有)并用'BLANK'填写na

df['Top item'] = (
    df.Item.str.split(',\s+', expand=True)
    .where(lambda x: x.applymap(lambda e: e in topitems))
    .bfill(1)
    .fillna('BLANK')
    [0]
)

    Item        Top item
0   Toy         Toy
1   Car, Toy    Car
2   Buses, Car  Car
3   Bike        Bike
4   Barbie      BLANK
5   Lorri       BLANK

【讨论】:

    【解决方案3】:

    解决方案:您可以使用 pop(0) 方法从列表中弹出第一个元素。

    import pandas as pd
    
    df = pd.DataFrame({'items':[
                            ['Toy'],
                            ['Car', 'Toy'],
                            ['Buses', 'Car'],
                            ['Bike'],
                            ['Barbie'],
                            ['Lorri'],
                        ]})
    
    topitems = ['Toy', 'Bike', 'Car']
    def pop_first_element(row):
        item = row['items']
        matched_list = [el for el in item if el in topitems]
        return [matched_list.pop(0)]if matched_list else ['BLANK']
    
    
    df['KEYWORDS'] = df.apply(pop_first_element , axis=1)
    

    【讨论】:

      猜你喜欢
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多