python pandas 行翻转_用pandas将行旋转成列

在构建用户-用户协同推荐系统时,使用Python的pandas库尝试将大型数据帧(700000行)的行转换为列,遇到"值错误:未堆叠的数据帧太大,导致int32溢出"的问题。数据源自MySql数据库,涉及`readData`函数进行数据读取和DataFrame创建。系统环境为Python 3.6、Win7系统和16GB内存,pandas版本为0.24.2。目前的解决办法尚未提及。
摘要由CSDN通过智能技术生成

我正在学习创建一个用户-用户协同推荐系统,在这个系统中,我借助Python:MySql_Connector从MySql数据库读取隐式数据。有了购买数据,我试图创建一个user*item rating matrix,对于这个矩阵,我用pandas将行(700000行)旋转成列。对整个数据帧运行pivot时出现以下错误。在

“值错误:未堆叠的数据帧太大,导致int32溢出”import mysql.connector

import pandas as pd

import numpy as np

from mysql.connector import errorcode

def readData():

try:

mySQLConnection = mysql.connector.connect(host='localhost',

database='testdb',

user='user',

password='pwd')

cursor = mySQLConnection.cursor(prepared=True)

sql_select_query = """""" #Removed the select query

cursor.execute(sql_select_query)

record = cursor.fetchall()

return record

except mysql.connector.Error as error:

print("Failed to get record from database: {}".format(error))

finally:

# closing database connection.

if (mySQLConnection.is_connected()):

cursor.close()

mySQLConnection.close()

print("connection is closed")

data = readData()

df = pd.DataFrame(data,columns=['user_id','product_id','purchase_count'])

data_pivot = pd.pivot_table(df,index=['user_id'],columns=df['product_id'])

#print(data_pivot.to_string())

python_版本:3.6

操作系统:win7

内存:16gb

pandas_版本:0.24.2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值