import pandas as pd
import numpy as np
# pandas基本数据结构
# Series 一维数组,与numpy中的一维array相似,二者与pyhton列表也相近。Series能保存不同数据类型,字符串,boolean值,数字等都能保存在series中
# Dateframe 二维的表格型数据结构。很多功能与R中date.frame类似。可以将DateFrame理解为Series的容器。以下的内容主要以DateFrame为主
# Series
# 一维Series可以用一维列表初始化; 索引可以自己设置
s = pd.Series([1,2,3])
'''0 1
1 2
2 3
dtype: int64'''
s = pd.Series([1,2,4],index =['a','b','c'])
'''a 1
b 2
c 4
dtype: int64'''
# 当使用其他字符作为索引时,切片操作为双闭合,两边端点元素都要取
# 查看数据索引:行标签
s.index # Index(['a', 'b', 'c'], dtype='object')
s.values # array([1, 2, 4], dtype=int64)
# 索引赋值
s.index.name='索引'
'''索引
a 1
b 2
c 4
dtype: int64'''
s.index=list('012')
'''0 1
1 2
2 3
dtype: int64'''
# DateFrame类型 这是个二维结构,这里首先构建一组时间序列,作为我们第一维的下标:
date = pd.date_range('20220423',periods=5)
'''DatetimeIndex(['2022-04-23', '2022-04-24', '2022-04-25', '2022-04-26',
'2022-04-27'],
dtype='datetime64[ns]', freq='D')'''
a = np.random.randint(1,10,30)
a.shape = 5,6
DF = pd.DataFrame(a,index=date,columns=list('ABCDEF')) # 第一个参数为二维数组,index,columns为索引,默认情况下他们的值由0开始的数字替代
''' A B C D E F
2022-04-23 9 4 8 4 3 1
2022-04-24 3 7 2 7 2 8
2022-04-25 1 3 4 5 1 3
2022-04-26 3 5 8 1 3 2
2022-04-27 5 3 2 4 6 9'''