pandas中的sql查询

2024-09-27 07:33:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类似这样的sql查询:

display = pd.read_sql_query("""
SELECT UserId, ProductId, ProfileName, Time, Score, Text, COUNT(*)
FROM Reviews
GROUP BY UserId
HAVING COUNT(*)>1
""", con)

有人能帮我用熊猫的代码吗?你知道吗

我试过:

cols = ['UserId', 'ProductId', 'ProfileName', 'Time', 'Score', 'Text']
df[cols].groupby('UserId').agg({'UserId':'count'})

这并没有给我从那个查询中得到的输出。你知道吗

这是数据集: https://www.kaggle.com/snap/amazon-fine-food-reviewsdatabase.sqlite


Tags: textfromreadsqltimecountdisplayquery
2条回答

因为你没有提供任何样本数据

可能的答案是

df[cols].groupby('UserId').count()

通过使用groupby和count,您可以很容易地做到这一点。你知道吗

df.groupby('UserId').count().reset_index()

相关问题 更多 >

    热门问题