python笔记:3.2.1.1pandas数据结构_DataFrame2

博客展示了将数组构造为DataFrame的过程。先是呈现了一组数值数组运行结果及相关DataFrame信息,后又给出股票数组,并将其构造为股票DataFrame,包含id、time、code等多列信息,还展示了该DataFrame的基本情况。
# -*- 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
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值