Pandas数据类型
目录
pandas数据类型
最重要的基础数据分析处理包
import numpy as np import pandas as pd
series的创建
无论是series还是dataframe都是在numpy的array基础上构建的
序列可以看成有顺序的字典或是有自定义索引的array
a=pd.Series([2,3,6],index=["i","u",3])
a
i 2 u 3 3 6 dtype: int64
a["u"]
3
??a[2]#双索引机制
??6
赋值
?
a[0]=8.0 #a[0]="aaa" a i 8 u 3 3 6 dtype: int64
序列的键:
a.values
array([8, 3, 6], dtype=int64)
序列的值:
a.index
Index(['i', 'u', 3], dtype='object')
字段形式的序列:
b=pd.Series({"w":2,2:4,3:3,"o":5,"r":6})#字典形式
b
w 2 2 4 3 3 o 5 r 6 dtype: int64
a=pd.Series([2,4,3,5,6])#会自动添加索引
a
0 2 1 4 2 3 3 5 4 6 dtype: int64
b=pd.Series({"w":2,2:4,3:3,"o":5,"r":6},index=["r","o"])#只会选取index中的索引
b
r 6 o 5 dtype: int64
dataframe的创建
一个二维数组并且横向与纵向都有自定义索引
ataframe可以看成一系列有相同索引的series
np.random.randn(6,3)
array([[-1.56404553, 3.35237792, 1.07003071], [-0.60057412, 0.28439735, 1.20820551], [ 0.89463854, -0.66382496, -1.61231831], [-0.68544236, -0.19163908, 0.46351491], [ 0.95863328, -0.21615597, -1.75244437], [ 0.93651183, -1.26155908, 0.12069469]])
a=pd.DataFrame(np.random.randn(6,3),index=range(2,8), columns=["a","b","c"])#相对数组增加了横向和纵向的自定义索引,双索引机制
a
a b c 2 0.720971 1.611298 0.522565 3 0.195510 -0.059229 2.137468 4 -0.609971 0.362206 -0.799471 5 -0.198100 -0.210045 1.171991 6 -2.704150 -0.619914 0.121779 7 0.307841 -0.019757 -2.454370
a["b"]
2 1.611298 3 -0.059229 4 0.362206 5 -0.210045 6 -0.619914 7 -0.019757 Name: b, dtype: float64
- a[0]
所有的值:
a.values
array([[ 0.05118313, 0.39638064, 0.38787328], [ 0.55483877, -1.17624348, 0.8606569 ], [-0.90502513, 2.06223449, -0.27556449], [-0.45554163, 0.007331 , -0.89722409], [-1.2040777 , 0.20244219, 0.51471742], [-0.44531058, -0.49707473, -1.94881836]])
所有的行索引:
a.index
RangeIndex(start=2, stop=8, step=1)
所有的列索引:
a.columns
Index(['a', 'b', 'c'], dtype='object')
a=pd.DataFrame(np.random.randn(6,3), columns=["a","b","c"])
a
a b c 0 1.664421 0.495775 -0.945343 1 -0.443564 2.499683 0.145670 2 -0.385458 -0.146777 0.397846 3 -0.307054 0.404264 -0.754313 4 -0.473609 -0.243507 -0.583231 5 -2.057494 -1.082490 0.376665
隐式索引
a=pd.DataFrame(np.random.randn(6,3))#会自动把隐式索引添加上去
a
0 1 2 0 2.750190 -2.037230 1.342428 1 0.004734 -0.160271 -0.787664 2 0.166225 0.677395 1.055825 3 -0.004122 0.418734 2.593051 4 -0.641335 -0.828338 0.882715 5 -0.802709 0.019870 0.257846
通过字典定义dataframe
b=pd.DataFrame({"a":[2,3],"b":[3,4]})#通过字典定义dataframe b
a b 0 2 3 1 3 4
通过序列生成数据框
b=pd.Series({"w":2,2:4,3:3,"o":5,"r":6})#通过序列生成数据框
pd.DataFrame(b)
0
w 2
2 4
3 3
o 5
r 6
pd.DataFrame({"c1":b,"c2":b},index=["w","o"])#通过series定义dataframe
c1 c2 w 2 2 o 5 5
pd.DataFrame([b])#注意这里转置了
w 2 3 o r 0 2 4 3 5 6
通过文件读入dataframe
注意源文件是否有列标,没有的时候可以自己用names补充
r1=pd.read_csv(r"t_alibaba_data3.txt",names=["user","brand","behavr","date"],sep="\t",dtype={"behavr":int})
pandas会自己判断数据类型,但是有时也需要自己额外指定数据类型
r1.head()
user brand behavr date 0 10944750 13451 0 06/04 1 10944750 13451 2 06/04 2 10944750 13451 2 06/04 3 10944750 13451 0 06/04 4 10944750 13451 0 06/04
r1.date
索引
实现过滤的原理
a1=pd.Series([2,3,6,0],index=[7,9,3,2])
b1=pd.Index([0,1,2,3])#也可以通过Index自定义索引
a1.index
Int64Index([7, 9, 3, 2], dtype='int64')
b1
Int64Index([0, 1, 2, 3], dtype='int64')
a1.index&b1
Int64Index([3, 2], dtype='int64')
a1.index|b1
Int64Index([0, 1, 2, 3, 7, 9], dtype='int64')
a1.index^b1
Int64Index([0, 1, 7, 9], dtype='int64')
a1[a1.index&b1]
3 6 2 0 dtype: int64
b1[0]
b1[0]=9#不可更改