Python 未提供所需的输出(ValueError:以10为基数的int()的文本无效:';198 | 260 | 157 | 136 | 136';)

Python 未提供所需的输出(ValueError:以10为基数的int()的文本无效:';198 | 260 | 157 | 136 | 136';),python,pandas,recommendation-engine,recommender-systems,Python,Pandas,Recommendation Engine,Recommender Systems,我正在实现一个在Medium上找到的推荐系统示例 在数据准备中,它具有这种“事务”结构,由熊猫读取csv: print(transactions.shape) transactions.head() 输出如下所示: (62483, 2) customerId products 0 0 20 1 1 2|2|23|68|68|111|29|86|107|152 2 2 111|107|29|11|11|11|33|23 3 3 164|227 4 5 2

我正在实现一个在Medium上找到的推荐系统示例

在数据准备中,它具有这种“事务”结构,由熊猫读取csv:

print(transactions.shape)
transactions.head()
输出如下所示:

(62483, 2)
customerId  products
0   0   20
1   1   2|2|23|68|68|111|29|86|107|152
2   2   111|107|29|11|11|11|33|23
3   3   164|227
4   5   2|2
(61282, 3)
customerId  productId   purchase_count
0   0   198|260|157|136|136 1
1   0   19|19|19    1
2   0   1|1|31|31   1
3   0   20  1
4   0   216|52|260|93|93|93 1
需要将其拆分为“数据”结构,可能如下所示:

(62483, 2)
customerId  products
0   0   20
1   1   2|2|23|68|68|111|29|86|107|152
2   2   111|107|29|11|11|11|33|23
3   3   164|227
4   5   2|2
(61282, 3)
customerId  productId   purchase_count
0   0   198|260|157|136|136 1
1   0   19|19|19    1
2   0   1|1|31|31   1
3   0   20  1
4   0   216|52|260|93|93|93 1

文章中的代码是:

data = pd.melt(transactions.set_index('customerId')['products'].apply(pd.Series).reset_index(), 
             id_vars=['customerId'],
             value_name='products') \
    .dropna().drop(['variable'], axis=1) \
    .groupby(['customerId', 'products']) \
    .agg({'products': 'count'}) \
    .rename(columns={'products': 'purchase_count'}) \
    .reset_index() \
    .rename(columns={'products': 'productId'})
data['productId'] = data['productId'].astype(np.int64)
但是,当我运行它时,输出给我这个错误“ValueError:invalid literal for int(),以10为底:‘198 | 260 | 157 | 136 | 136’”,因为它没有拆分'productId'列中的值

我得到了这样的东西:

(62483, 2)
customerId  products
0   0   20
1   1   2|2|23|68|68|111|29|86|107|152
2   2   111|107|29|11|11|11|33|23
3   3   164|227
4   5   2|2
(61282, 3)
customerId  productId   purchase_count
0   0   198|260|157|136|136 1
1   0   19|19|19    1
2   0   1|1|31|31   1
3   0   20  1
4   0   216|52|260|93|93|93 1

有什么想法吗?

对于那些面临相同问题的人,在使用o melt函数之前,使用以下代码解决了问题:

transactions['products'] = transactions['products'].apply(lambda x: [int(i) for i in x.split('|')])

“因为它没有拆分'productId'列中的值”
您要求它在哪里执行此操作?我只是按照教程进行操作。在使用给定代码执行单元格后,根据帖子,我应该会得到类似于发布的图像的东西。。。我对熊猫不熟悉。我真的不知道为什么我得到了一些不同于帖子的东西。。。