1.Series一维数据结构
#导入pandas、numpy 安装:pip3 install pandas pip install numpy
import pandas as pd
import numpy as np
a = np.arange(5) #arange(5)指0~5范围,但不包含5,如[0 1 2 3 4]
b = pd.Series(a,index=['a','b','c','d','e']) #index自定义索引
# a 0
# b 1
# c 2
# d 3
# e 4
# dtype: int32
print(b[0]) #使用下标检查 值:0
print(b['a']) #使用索引检索 值:0
2.DataFrame二维数据结构
import pandas as pd
import numpy as np
a = np.arange(8).reshape(4,2) #arange(8)0~7 reshape(4,2)四行两列
#columns自定义列名 dtype自定义数据类型
b = pd.DataFrame(a,columns=['num','num01'],dtype=float,index=['a','b','c','d'])
print(b)
# num num01
# a 0.0 1.0
# b 2.0 3.0
# c 4.0 5.0
# d 6.0 7.0
import pandas as pd
import numpy as np
#字典
a = {'name':["zs",'lisi','ww'],'age':[23,44,43]}
b = pd.DataFrame(a,dtype=float) #DataFrame()二维矩阵 dtype定义数据类型 字典键被用作列名
# name age
# 0 zs 23.0
# 1 lisi 44.0
# 2 ww 43.0
data = [{'a':11,'c':43},{'a':33,'b':44,'c':66}]
b = pd.DataFrame(data,index=['a','b'],columns=['a','b'])
# a b
# a 11 NaN
# b 33 44.0
a = pd.Series([11,22,33],index=['a','b','c'])
b = pd.Series([33,44,55,77],index=['a','b','c','d'])
c = {'one':a,'two':b}
d = pd.DataFrame(c)
# one two
# a 11.0 33
# b 22.0 44
# c 33.0 55
# d NaN 77
print(d['one']) 查询列
# a 11.0
# b 22.0
# c 33.0
# d NaN
f = pd.Series([77,88,99,77],index=['a','b','c','d'])
d['three'] = f #新增列
# one two three
# a 11.0 33 77
# b 22.0 44 88
# c 33.0 55 99
# d NaN 77 77
del d['one'] #删除列 pop d['one']
# two three
# a 33 77
# b 44 88
# c 55 99
# d 77 77
g = d.loc['a'] #行查询
# two 33
# three 77
# Name: a, dtype: int64
h = d.iloc[0] #列查询
# a 77
# b 88
# c 99
# d 77
# dtype: int64
g = d[1:3] #切片行
# two three
# b 44 88
# c 55 99
o = pd.DataFrame([[66,66]],columns=['two','three'],index=['e'])
df = d.append(o) #添加行
# two three
# a 33 77
# b 44 88
# c 55 99
# d 77 77
# e 66 66
df = df.drop('a') #删除行
# two three
# b 44 88
# c 55 99
# d 77 77
# e 66 66
3.Panel三维数据结构
import pandas as pd
import numpy as np
a = np.random.randint(4,5,6)
p = pd.Panel(a)