美文网首页
4dataframe 统计分类电影

4dataframe 统计分类电影

作者: mll_497f | 来源:发表于2021-06-30 21:26 被阅读0次

    import pandas pd

    from matplotlib import pyplot as pyplot

    df=pd.read_csv("movie.csv")

    #统计分类列表

    temp_data=df['Genre'].str.split(",").tolist() #[[],[],[]]

    genre_list=list(set[j for i in temp_data  for j in i])

    #生成0的列

    zeros_colmns=pd.DataFrame(np.zeros(temp_data.shape[0],len(genre_list)),columns=genre_list)

    for i in range(df.shape[0]):

          #temp_data["sci-fi","muc"] 

          # temp_data 是列表, 取第2行里的多列zero_colmns.loc[1,["sci-fi","muc"] ]=1

          zeros_colmns.loc[i,temp_data[i]]=1

    #统计每个分类电影的数量和

    genre_count=zeros_df.sum(axis=0)

    #统计

    genre_count=genre_count.sort_values()

    #绘图

    plt.figure(figsize=(20,8),dpi=80)

    _x=genre_count.index

    _y=genre_count.values

    plt.bar(range(len(_x)),_y)

    plt.xticks(range(len(_x)),_x)

    plt.show()

    2..join()  默认情况下它是把行行索引相同的数据和并到一起

    t.join(t2)  在t的基础上把t2以列添加到t的列上,如果没有则是nan(如果点后的行数比点前的行数多,则只显示点前的。如t.join(t2),如果点前的行数比点后的多则会出现nan ,如t2.join(t))

    merge 不懂~~~~ armin不懂

    相关文章

      网友评论

          本文标题:4dataframe 统计分类电影

          本文链接:https://www.haomeiwen.com/subject/bxfqcktx.html