根据列值返回用户列表

时间:2018-02-21 10:02:09

标签: python pandas

我有一个数据网格,其中每一行都是使用的,每列都是他们购买的产品类型,这里是修剪版本,全表有200多列:

UserID  total   purchase_range  tshirts jeans   jumpers shoes   scarves belts   hats    coats   chinos  socks
a3470c41-d349-4f5c-bd2d-ed58d2959758    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
02dbb049-f28e-4637-9e35-3bce06b65727    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
9803c98a-890c-4b99-b32a-f34658b1bddd    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
5e19940d-d981-4e42-900c-242687d37ae0    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
8cf37896-b675-491e-a06a-6282966d8a43    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
931f63a1-456f-4ff4-b0c5-4474a5e4a75d    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
552b12dc-5ea1-49d1-ab08-9b7c688df03c    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
bbe5d4f5-8b32-44a3-bb89-eed8304111e7    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
09d15874-1cdc-43aa-9761-a3287faed610    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
9825692b-912b-45e1-b3ae-f18d7eda8700    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
030dccda-7de2-4293-aee6-ad079f6f0feb    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
4388651d-041a-45d8-b7fe-1894003ce4f2    1   Narrow  0   0   1   0   0   0   0   0   0   0   0
06c643f3-b93b-49df-974a-8d5c2cf97e8b    1   Narrow  0   0   1   0   0   0   0   0   0   0   0
6a6f0f75-5970-470f-b1f5-a299a26e0468    1   Narrow  0   0   0   0   0   0   0   0   0   0   0
62739f9f-e1aa-4139-b26e-0df8679aee3d    1   Narrow  0   0   1   0   0   0   0   0   0   0   0
4d0605b5-b043-466c-a13c-17a17b6a7ba8    1   Narrow  0   0   0   1   0   0   0   0   0   0   0
9d6e6eba-53c2-4f23-ab25-3c169c35cf2f    1   Narrow  0   0   0   0   0   0   0   0   0   0   0

返回购买特定产品类型或其组合的userId列表的最佳方法是什么?我是否需要为每个类别建立一个空白列表,然后使用for循环迭代它以计算该列中“1”的数量?如果是这样,我如何返回实际的用户ID而不是计数?是否有更优雅/灵活的方式来做到这一点?

2 个答案:

答案 0 :(得分:2)

只需使用标准布尔逻辑和索引:

鞋子和牛仔裤的组合:

indices = (df['shoes'] > 0) & (df['jeans'] > 0)
print(df['userid'][indices])

输出:

dtype: bool
0    <some-id>
2    <other-id>
Name: userid, dtype: int64

(那个输出是Series。使用df['userid'][indices].values得到一个带有纯索引的numpy数组。)

答案 1 :(得分:1)

这是一种方法。这种方法的优点是可以提供任意组合的产品来检查。

import pandas as pd
import numpy as np

df = pd.DataFrame({'UserID': {0: 'a3470c41-d349-4f5c-bd2d-ed58d2959758',
                              1: '02dbb049-f28e-4637-9e35-3bce06b65727',
                              2: '9803c98a-890c-4b99-b32a-f34658b1bddd'},
                   'belts': {0: 0, 1: 0, 2: 1},
                   'chinos': {0: 0, 1: 0, 2: 0},
                   'coats': {0: 1, 1: 1, 2: 1},
                   'hats': {0: 1, 1: 1, 2: 0},
                   'jeans': {0: 0, 1: 1, 2: 0},
                   'jumpers': {0: 0, 1: 1, 2: 1},
                   'purchase_range': {0: 0, 1: 0, 2: 0},
                   'scarves': {0: 0, 1: 0, 2: 0},
                   'shoes': {0: 0, 1: 0, 2: 0},
                   'socks': {0: 0, 1: 0, 2: 0},
                   'tshirts': {0: 1, 1: 0, 2: 1}})

def get_ids(lst):
    return df.loc[np.logical_and.reduce([df[i] for i in lst]), 'UserID'].tolist()

get_ids(['tshirts', 'hats'])

# ['a3470c41-d349-4f5c-bd2d-ed58d2959758']