zoukankan      html  css  js  c++  java
  • Pandas笔记:DataFrame基本操作

    import numpy as np
    import pandas as pd
    data = np.random.normal(0, 1, (10, 5))
    index = ["股票{}".format(i) for i in range(10)]
    date = pd.date_range(start="20180101", periods=5, freq="B")
    data_pd = pd.DataFrame(data, index, date)   # 列:index   行:date   数据:data
    print("------------------显示生成的数据:index,date,data-------------------")
    print(data_pd.head())   # 获取前5行数据
    print("------------------转置并获取前3行数据---------------------------------")
    print(data_pd.T.head(3))
    print("------------------显示后面5行数据-------------------------------------")
    print(data_pd.tail())
    print("------------------设置索引------------------------------------------")
    print(data_pd.index)
    print("------------------重设索引-------------------------------------")
    print(data_pd.reset_index(drop=False).head())
     

    运行结果:

    ------------------显示生成的数据:index,date,data-------------------
         2018-01-01  2018-01-02  2018-01-03  2018-01-04  2018-01-05
    股票0   -1.506607    0.858618   -1.000629   -0.797692   -0.775486
    股票1    1.029000    0.257812   -0.603442    1.181301    0.831904
    股票2    0.793368    1.294983    0.114872    0.637446    0.929683
    股票3   -0.242412   -0.202032   -1.355949    1.135424    2.406216
    股票4    1.042224   -0.465189   -3.235827    0.438931    0.366087
    ------------------转置并获取前3行数据---------------------------------
                     股票0       股票1       股票2  ...       股票7       股票8       股票9
    2018-01-01 -1.506607  1.029000  0.793368  ... -0.590076  0.272599  0.294911
    2018-01-02  0.858618  0.257812  1.294983  ...  0.860489 -0.155215 -1.366205
    2018-01-03 -1.000629 -0.603442  0.114872  ... -0.072485 -0.010132  0.260509
     
    [3 rows x 10 columns]
    ------------------显示后面5行数据-------------------------------------
         2018-01-01  2018-01-02  2018-01-03  2018-01-04  2018-01-05
    股票5   -0.916197    0.495509   -0.138883    0.815526   -0.288074
    股票6   -0.145641   -2.434759   -0.484725    0.805658    1.682301
    股票7   -0.590076    0.860489   -0.072485   -1.134887   -0.700504
    股票8    0.272599   -0.155215   -0.010132    2.404830   -1.011119
    股票9    0.294911   -1.366205    0.260509    0.879498    0.619766
    ------------------设置索引------------------------------------------
    Index(['股票0', '股票1', '股票2', '股票3', '股票4', '股票5', '股票6', '股票7', '股票8', '股票9'], dtype='object')
    ------------------重设索引-------------------------------------
      index  2018-01-01 00:00:00  ...  2018-01-04 00:00:00  2018-01-05 00:00:00
    0   股票0            -1.506607  ...            -0.797692            -0.775486
    1   股票1             1.029000  ...             1.181301             0.831904
    2   股票2             0.793368  ...             0.637446             0.929683
    3   股票3            -0.242412  ...             1.135424             2.406216
    4   股票4             1.042224  ...             0.438931             0.366087
     
    [5 rows x 6 columns]
     
  • 相关阅读:
    c#基础问题笔记(一)
    自动化技术中的进给电气传动研习笔记2
    自动化技术中的进给电气传动研习笔记1
    汉字在电脑中是如何存储与编码的呢?
    三十分钟掌握STL
    python练习:函数2
    python练习:函数3
    Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form ResumeForm needs updating.
    vue 数组对象取对象的属性: Cannot read property 'xxxx' of undefined
    python练习:函数4
  • 原文地址:https://www.cnblogs.com/jumpkin1122/p/11509769.html
Copyright © 2011-2022 走看看