pandas unstack

import pandas as pd
import numpy as np
%matplotlib inline
df=pd.read_csv("./ant-learn-pandas/datas/movielens-1m/ratings.dat",
sep="::",
engine='python',
names="UserID::MovieID::Rating::Timestamp".split("::"))
df.head()
df['pdate']= pd.to_datetime(df['Timestamp'], unit='s')
df['pmonth']=pd.to_datetime(df['Timestamp'], unit='s').dt.month
df.head(20)
#  
grouped_stats = df.groupby(['pmonth','Rating']).agg({'UserID':'sum'})
grouped_stats.head(20)
df_stack=grouped_stats.unstack()
df_stack.head(20)
df_stack.plot()
df_stack.stack().head(20)



# 将时间戳转换为日期dfl'pdate']= pd.to datetime(df['Timestamp'], unit='s')df['pmonth']=pd.to datetime(df['Timestamp'l, unit='s').dt.monthdf.head(20)
#df _grouped=df.groupby('pmonth','Rating').agg({df['UserId' ]: ['sum' ]})
#grouped agg = df.groupby('pmonth')['Rating'].agg(su=('UserID',))grouped stats = df.groupby(['pmonth','Rating'l).agg({ 'userID': 'sum'})

               
# print(grouped_agg)

 

posted @ 2024-08-19 00:12  残阳飞雪  阅读(3)  评论(0编辑  收藏  举报