美文网首页
Pandas之pivot_table数据透视

Pandas之pivot_table数据透视

作者: IntoTheVoid | 来源:发表于2020-04-24 15:34 被阅读0次
    import pandas as pd
    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    %matplotlib inline
    import warnings
    warnings.filterwarnings('ignore')
    
    df = pd.DataFrame({
              'asin':list('ABCDA'),
              'rank':[1,1,2,3,3],
              'cate_name':['a','b','c','a','d'],
              'date':['2010-01-01','2010-01-03','2010-01-03','2010-01-04','2010-01-06']})
    df
    
    image.png
    df['scaled_rank'] = (df['rank'] - df["rank"].max()).abs()
    df
    
    image.png
    df.date = pd.to_datetime(df.date)
    time_range = pd.DataFrame(pd.date_range('2010-01-01','2010-01-07',freq='D'), columns=['date']).set_index("date")
    time_range
    
    image.png
    df = pd.pivot_table(df, columns='asin', index='date')
    df
    
    image.png
    df = df.merge(time_range, how="right", left_index=True, right_index=True)
    df[('scaled_rank',)] = df[('scaled_rank',)].fillna(0)
    df.T
    
    image.png

    相关文章

      网友评论

          本文标题:Pandas之pivot_table数据透视

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