zoukankan      html  css  js  c++  java
  • pandas 制造数据和简单的画图

    # 0 数据制造
    import pandas as pd
    df1 =pd.DataFrame(10*np.random.rand(7,3),index=["A","B","C","D","E","F","G"],columns=["ALL","Fail","PASS"])
    df1=pd.DataFrame(np.arange(16).reshape((4,4)),index=['a','b','c','d'],columns=['one','two','three','four'])
    # 2层索引
    df1=pd.DataFrame(np.arange(16).reshape((4,4)),index=[['a','b','c','d'],['A','B','C','D']],columns=['one','two','three','four'])
    
    df2 = pd.DataFrame({
        "pdate": ["2019-12-01", "2019-12-02", "2019-12-04", "2019-12-05"],
        "pv": [100, 200, 400, 500],
        "uv": [10, 20, 40, 50],
    })
    
    data = {'水果':['苹果','梨','草莓'],
           '数量':[3,2,5],
           '价格':[10,9,8]}
    df3 = pd.DataFrame(data)
    
    
    data = {'数量':{'苹果':3,'梨':2,'草莓':5},
           '价格':{'苹果':10,'梨':9,'草莓':8}}
    df4 = pd.DataFrame(data)
    
    
    df5= pd.DataFrame([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], \
    columns=["col1", "col2", "col3", "col4"]) 
    
    
    s1 = pd.Series(
        [
            "this is a regular sentence",
            "https://docs.python.org/3/tutorial/index.html",
            np.nan
        ])
    
    
    
    # 1 简单的画图 pyecharts(pip install pyecharts)
    from pyecharts.charts import Line
    from pyecharts import options as opts
    
    df=pd.DataFrame(10*np.random.rand(7,3),index=["A","B","C","D","E","F","G"],columns=["ALL","Fail","PASS"])
    
    # 折线图
    line = Line()
    # x轴
    line.add_xaxis(df.index.to_list())
    # 每个y轴
    line.add_yaxis("失败", df["Fail"].round(2).to_list())
    line.add_yaxis("成功", df["PASS"].round(2).to_list())
    
    # 图表配置
    line.set_global_opts(
        title_opts=opts.TitleOpts(title="股票2019年"),
        tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross")
    )
    # 渲染数据
    line.render_notebook()
    line.render()
    
    
    
    
    # 2 造数据和简单的画图 plot()
    import pandas as pd
    %matplotlib inline
    
    df = pd.DataFrame({
        "pdate": ["2019-12-01", "2019-12-02", "2019-12-04", "2019-12-05"],
        "pv": [100, 200, 400, 500],
        "uv": [10, 20, 40, 50],
    })
    
    df.set_index("pdate").plot()
    
    
    # 1
    df1=pd.DataFrame(10*np.random.rand(7,3),index=["A","B","C","D","E","F","G"],columns=["ALL","Fail","PASS"])
    df2=pd.DataFrame(10*np.random.rand(7,3),index=["A","B","C","D","E","F","G"],columns=["ALL","Fail","PASS"])
    
    def prep_df(df, name):
        df = df.stack().reset_index()
        df.columns = ['c1', 'result', 'values']
        df['DF'] = name
        return df
    
    df1 = prep_df(df1, 'DF1')
    df2 = prep_df(df2, 'DF2')
    df = pd.concat([df1, df2])
    
    
    
    # 2
    df = pd.read_csv('xxx.csv',encoding = "utf-8",delimiter=",",error_bad_lines=False)
    df.set_index(pd.to_datetime(df["time"]),inplace=True)
    source = df.sort_index()[['time','all case name','pass case','failed case']]
    source = source.melt('time', var_name='category', value_name='y')
    
    
    # 3
    import pandas as pd
    data = {'水果':['苹果','梨','草莓'],
           '数量':[3,2,5],
           '价格':[10,9,8]}
    df = pd.DataFrame(data)
    print(df)
    print('*********')
    
    
    data = {'数量':{'苹果':3,'梨':2,'草莓':5},
           '价格':{'苹果':10,'梨':9,'草莓':8}}
    df = pd.DataFrame(data)
    print(df)
    print('*********')
    
    data = {'水果':pd.Series(['苹果','梨','草莓']),
           '数量':pd.Series([3,2,5]),
           '价格':pd.Series([10,9,8])}
    df = pd.DataFrame(data)
    print(df)
    
    
  • 相关阅读:
    java---随机点名
    java实现窗体JFrame加载动态图片
    微信小程序--登陆页面
    java基础--随机数 猜数字游戏
    jQuery表单验证
    js获取Id,标签,类的几种选择器封装
    轮播图
    关于mysql安全
    PHP5.4开启zend opcache缓存
    ccs3中icon转换为字体的方法
  • 原文地址:https://www.cnblogs.com/amize/p/14036824.html
Copyright © 2011-2022 走看看