Pandas的基本使用

来源:互联网 发布:淘宝小号查询源码 编辑:程序博客网 时间:2024/05/29 04:47

import numpy as npimport pandas as pd#首先创建索引dates = ['2017-01-01', '2017-02-1', '2017-03-01', '2017-04-01', '2017-05', '2017-06-01']dates = pd.to_datetime(dates)print(dates)
DatetimeIndex(['2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01',
               '2017-05-01', '2017-06-01'],
              dtype='datetime64[ns]', freq=None)


#创建DataFrame对象df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))print(df)
                   A         B         C         D
2017-01-01  0.470732  1.261291 -1.478082 -0.117008
2017-02-01  0.439358  1.373300  1.463247 -1.508368
2017-03-01  0.894984  1.280350 -0.660829 -1.133081
2017-04-01  1.618866  0.835632  0.547147  0.023842
2017-05-01 -0.129568 -0.540310  1.757176  0.867668
2017-06-01  0.358594  0.656273  1.240374 -1.172496

print(df.columns)
Index(['A', 'B', 'C', 'D'], dtype='object')

print(df.index)
DatetimeIndex(['2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01',
               '2017-05-01', '2017-06-01'],
              dtype='datetime64[ns]', freq=None)

print(df.values)
[[ 0.47073212  1.26129099 -1.47808151 -0.11700753]
 [ 0.43935763  1.37329994  1.46324734 -1.50836817]
 [ 0.89498355  1.28035036 -0.66082914 -1.13308051]
 [ 1.61886613  0.83563176  0.54714678  0.02384197]
 [-0.12956774 -0.54030978  1.7571758   0.86766752]
 [ 0.35859426  0.65627347  1.24037368 -1.1724956 ]]


print(df.describe())
              A         B         C         D
count  6.000000  6.000000  6.000000  6.000000
mean   0.608828  0.811089  0.478172 -0.506574
std    0.592908  0.719697  1.289665  0.912271
min   -0.129568 -0.540310 -1.478082 -1.508368
25%    0.378785  0.701113 -0.358835 -1.162642
50%    0.455045  1.048461  0.893760 -0.625044
75%    0.788921  1.275586  1.407529 -0.011370
max    1.618866  1.373300  1.757176  0.867668





















0 0
原创粉丝点击