數據分組分析—-groupbyspa
代碼功能:code
對於綜合表格data,基於title進行分組處理,並統計每一組的size,獲得的是一個series序列,此序列能夠放入索引中使用,index()blog
import pandas as pd unames = ['user_id', 'gender', 'age','occupation','zip'] users = pd.read_table('users.dat', sep='::',header=None, names=unames) rnames = ['user_id', 'movie_id', 'rating', 'timestamp'] ratings = pd.read_table('ratings.dat', sep='::', header=None, names=rnames) mnames = ['movie_id', 'title', 'genres'] movies = pd.read_table('movies.dat', sep='::', header=None, names=mnames) data = pd.merge(pd.merge(ratings,users),movies) mean_ratings = pd.pivot_table(data,index=['title'],values='rating',columns='gender') print(mean_ratings[:10]) ratings_by_title = data.groupby('title').size() print(ratings_by_title[:10]) active_titles = ratings_by_title.index[ratings_by_title >= 250] print(active_titles)
對獲得的序列使用index()過濾處理後,不知道是個什麼內行的量,打印出來結果以下:索引