Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从数据帧创建篮子-非标准事务数据集_Python_Pandas - Fatal编程技术网

Python 从数据帧创建篮子-非标准事务数据集

Python 从数据帧创建篮子-非标准事务数据集,python,pandas,Python,Pandas,我正在使用熊猫制作一个数据集。数据集的格式如下: 用户id产品id 用户1产品1 用户2产品3 用户1产品2 或许这更清楚: 数据集=[[user1,product1], [user2,product3], [用户1,产品2]] 我的目标是使用此数据集为要购买的产品提供建议。我将使用关联规则-apriori算法 由于我没有一个典型的交易数据集,其中包含多个产品一起购买(在相同的交易ID中),并且我只能使用该数据集,因此我考虑如果用户1购买了产品1和产品2,那么产品1和产品2一起购买 之后,我将使

我正在使用熊猫制作一个数据集。数据集的格式如下:

用户id产品id

用户1产品1

用户2产品3

用户1产品2

或许这更清楚:

数据集=[[user1,product1], [user2,product3], [用户1,产品2]]

我的目标是使用此数据集为要购买的产品提供建议。我将使用关联规则-apriori算法

由于我没有一个典型的交易数据集,其中包含多个产品一起购买(在相同的交易ID中),并且我只能使用该数据集,因此我考虑如果用户1购买了产品1和产品2,那么产品1和产品2一起购买

之后,我将使用关联规则/apriori算法创建规则。。但要做到这一点,我需要数据。形式如下:

数据=[[product1,product2],[product2],[product3,product1,product2]]

因此,我需要以下格式的数据集:

数据集=[[user1,product1,product2],[user2,product3]]

之后,我可以进一步应用apriori。。一个热编码,发现频繁项目等

df.groupby(['user_id'])['product_id']

groupby无法应用,因为我必须应用函数。。此外,pivot
功能不起作用。。这些是我在尝试进行转换时唯一考虑的问题。

这可能不是最好的解决方案-也许更有经验的人可以提供合适的解决方案。我通过执行以下操作实现了您所需的输出:

# set user_id as index of dataframe
df.set_index('user_id', inplace=True)

dataset=[]
for u in df.index.unique():
    data = df.loc[u]['product_id']
    data = [data] if isinstance(data, str) else data.tolist()
    dataset.append([u]+data)
输出:

[['user1', 'product1', 'product2'], ['user2', 'product3']]

如果这回答了您的问题,请告诉我:)

IIUUC您可以通过
pd.crosstab

import pandas as pd
df = pd.DataFrame({'user_id': ['user1', 'user2', 'user1', 'user3', 'user3', 'user1', 'user2'],
                   'product_id': ['milk', 'eggs', 'milk', 'bread', 'butter', 'eggs', 'cheese']})

df1 = pd.crosstab(df.user_id, df.product_id).astype('bool').astype('int')
df1.columns.name=None
df1.index.name=None
df1现在是:

       bread  butter  cheese  eggs  milk
user1      0       0       0     1     1
user2      0       0       1     1     0
user3      1       1       0     0     0

如果需要该列表格式,您可以
groupby
+
apply(list)

或者,如果您不关心重复项:

df.groupby('user_id').product_id.apply(set)
#user_id
#user1       {milk, eggs}
#user2     {cheese, eggs}
#user3    {bread, butter}
#Name: product_id, dtype: object

也许可以解释一下你试图解决的实际问题?使数据帧(这有点不清楚)成为真正问题的一部分实际上可能不是问题的一部分。我猜你可以用一个
groupby.apply()
来做你想做的事情,而不需要做其他的
DataFrame
好的,我会解释我试图解释这个问题。。我需要一个特定的格式,以便能够使用。。我也不知道如何正确地命名这个问题。如果我理解正确,您要创建的
数据框将基本上为每个
用户id
设置一行,并为每个不同的
产品id
设置一列,其中1或0表示是否购买了该产品?是的。这应该是我将用于apriori算法的方法。这个答案很好-我今天也学到了,你可以做以下
df.groupby('user\u id')。product\u id.apply(list)
。谢谢分享:)我不知道交叉表!谢谢这样就不需要使用scikit learn进行一次热编码,因为它可以直接提供结果!解释手头的真正问题总是好的,因为有时候有一种更简单的方法可以达到你想要的目的:D
df.groupby('user_id').product_id.apply(set)
#user_id
#user1       {milk, eggs}
#user2     {cheese, eggs}
#user3    {bread, butter}
#Name: product_id, dtype: object