python笔记:3.2.1.1pandas数据结构

    xiaoxiao2022-07-05  151

    # -*- coding: utf-8 -*- """ Created on Wed May 22 17:07:10 2019 @author: User """ import pandas as pd import numpy as np numfram = np.random.randn(10, 5) framnum = pd.DataFrame(numfram) print(numfram) print(framnum.info()) print(framnum.dtypes) print('\n 前面股票数组构造为 DataFram:') stock=np.dtype([('id',np.str,5), ('time',np.str,10), ('code',np.str,10), ('open_p',np.float64), ('close_p',np.float64), ('low_p',np.float64), ('vol',np.int32), ('high_p',np.float64), ('col',np.int32)]) jd_stock=np.loadtxt('data\stock.csv', delimiter=',',dtype=stock) print('\n 股票数组:') print(jd_stock) jd=pd.DataFrame(jd_stock) print('\n 股票 DataFrame:') print(jd.head()) print(jd.info())

    运行:

    [[-0.62009017  0.70829966 -0.48548659  1.26310006 -0.48235138]  [-1.67862693 -0.97096625 -1.65126175  0.35602323  0.78619157]  [ 0.46876188  1.45965403  1.69822388  1.35285213  0.86966089]  [ 0.77324385  2.17588443 -0.49302096  0.85118577  0.08857271]  [-0.84212732 -0.85268892 -0.49219341 -0.59472765 -0.4099793 ]  [ 1.22608899  1.67942467 -0.09757688 -0.68517965  0.12559482]  [-1.89127552 -0.02755593 -0.17825539  0.15061576 -0.01835327]  [ 0.38320852  1.26878589 -1.0170889  -1.58483841 -1.52350518]  [-0.79898396  0.68955353 -1.94068854 -1.87484369 -1.4181755 ]  [-0.59692944 -0.46468301 -0.45258183 -0.61153849 -0.97766694]] <class 'pandas.core.frame.DataFrame'> RangeIndex: 10 entries, 0 to 9 Data columns (total 5 columns): 0    10 non-null float64 1    10 non-null float64 2    10 non-null float64 3    10 non-null float64 4    10 non-null float64 dtypes: float64(5) memory usage: 480.0 bytes None 0    float64 1    float64 2    float64 3    float64 4    float64 dtype: object

     前面股票数组构造为 DataFram:

     股票数组: [('1', '20130902', '600028', 4.41, 4.43, 4.37, 17275, 4.41, 392662)  ('2', '20130903', '600028', 4.41, 4.46, 4.4 , 19241, 4.45, 434177)  ('3', '20130904', '600028', 4.44, 4.49, 4.42, 20106, 4.47, 451470) ...  ('3980', '20190327', '600019', 7.14, 7.15, 7.08, 29373, 7.13, 412887)  ('3981', '20190328', '600019', 7.1 , 7.12, 7.05, 25452, 7.08, 359576)  ('3982', '20190329', '600019', 7.07, 7.25, 7.07, 54683, 7.23, 762021)]

     股票 DataFrame:   id      time    code  open_p  close_p  low_p    vol  high_p     col 0  1  20130902  600028    4.41     4.43   4.37  17275    4.41  392662 1  2  20130903  600028    4.41     4.46   4.40  19241    4.45  434177 2  3  20130904  600028    4.44     4.49   4.42  20106    4.47  451470 3  4  20130905  600028    4.47     4.48   4.42  15582    4.47  349997 4  5  20130906  600028    4.46     4.52   4.45  19101    4.50  425777 <class 'pandas.core.frame.DataFrame'> RangeIndex: 3982 entries, 0 to 3981 Data columns (total 9 columns): id         3982 non-null object time       3982 non-null object code       3982 non-null object open_p     3982 non-null float64 close_p    3982 non-null float64 low_p      3982 non-null float64 vol        3982 non-null int32 high_p     3982 non-null float64 col        3982 non-null int32 dtypes: float64(4), int32(2), object(3) memory usage: 249.0+ KB None  

    最新回复(0)