pandas常用数据类型有Series和DataFrame,Series为一维数组,DataFrame为二维,均为带有标签的数组。
Series有两种方法创建:通过列表或字典创建。
import pandas as pd
t = pd.Series([1,4,35,5,6])
print(t)
#取值大于5的索引和值
print(t[t>5])
#创建series时规定索引
t2 = pd.Series([1,4,35,5,6],index = list("abcde"))
print(t2,type(t2))
#改变数据类型
t3 = t2.astype("float")
print(t3)
0 1
1 4
2 35
3 5
4 6
dtype: int64
2 35
4 6
dtype: int64
a 1
b 4
c 35
d 5
e 6
dtype: int64 <class 'pandas.core.series.Series'>
a 1.0
b 4.0
c 35.0
d 5.0
e 6.0
#通过字典创建一维数组
temp_dict = {"name":"tom","age":30,"tel":"10086"}
t4 = pd.Series(temp_dict)
print(t4)
#引入string模块后,通过字典推导式创建字典,ascii_uppercase表示按顺序得出大写字母
a = {string.ascii_uppercase[i]:i for i in range(5)}
print(a)
b = pd.Series(a)
print(b)
c = pd.Series(a,index = list(string.ascii_uppercase[4:8]))
print(c)
name tom
age 30
tel 10086
dtype: object
{'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4}
A 0
B 1
C 2
D 3
E 4
dtype: int64
E 4.0
F NaN
G NaN
H NaN
dtype: float64
注:在pandas中当出现nan时,pandas会自动将类型由int改为float,numpy无此操作。
Series切片
temp_dict = {"name":"tom","age":30,"tel":"10086"}
t = pd.Series(temp_dict)
print(t)
print("*"*10)
#通过索引查找
print(t["age"])
print(t[["age","tel"]])
print("*"*10)
#通过位置查找
print(t[0])
print(t[0:2])
name tom
age 30
tel 10086
dtype: object
**********
30
age 30
tel 10086
dtype: object
**********
tom
name tom
age 30
dtype: object
DataFrame
创建DataFrame
注:list为行索引,clolumns为列索引,表示索引要以列表形式,用小括号。
a = np.arange(12).reshape(3,4)
t = pd.DataFrame(a,index = list("abc"),columns = list("wxyz"))
print(t)
#生成字典
d1 = {"name":["tom","jack"],"age":[17,20],"tel":[10086,10010]}
print(d1)
#将字典转换为DataFrame
d1 = pd.DataFrame(d1)
print(d1)
w x y z
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
{'name': ['tom', 'jack'], 'age': [17, 20], 'tel': [10086, 10010]}
name age tel
0 tom 17 10086
1 jack 20 10010
索引
a.loc通过标签索引数据,a.iloc通过位置索引数据
t = pd.DataFrame(np.arange(12).reshape(3,4),index = list("abc"),columns = list("wxyz"))
print(t)
#通过标签索引数据
print(t.loc["a","z"])
#索引a这一行,或print(t.loc["a",:])
print(t.loc["a"])
#取多行
print(t.loc[["a","c"]])
#通过位置索引
print(t.iloc[1:])
w x y z
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
3
w 0
x 1
y 2
z 3
Name: a, dtype: int32
w x y z
a 0 1 2 3
c 8 9 10 11
w x y z
b 4 5 6 7
c 8 9 10 11