最近几天学习了pandas的部分知识,总结如下:
一、数据结构
1、Series:一种类似于一维数组的对象,是由一组数据(各种Numpy数据类型)以及一组与之相关的数据标签(即索引)组成,仅由一组数据即可产生简单的Series
2、DataFrame:一个表格型数据结构,含有一组有序的列,每类可以是不同的值类型(数值、字符串、布尔值等),DataFrame既有行索引也有列索引,可以被看成是由Series组成的字典。
二、引入约定
import pandas as pd
from pandas import DataFrame,Series
三、Series 一维数组
1、通过一维数组创建Series
arr=np.arange(20)
print(arr)#建立一个数组
#out [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
series=Series(arr) #一个一维数组对象
print(series)
#out #第一列代表标签
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
dtype: int32
(1)查看索引列
print(series.index)
#output如下:
RangeIndex(start=0, stop=20, step=1)
#查看具体值
print(series.values)
#output:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
print(series.dtype)
#output:
int32
(2)数据index绑定
#index实现索引的绑定
series1=Series([70,82,68],index=['张三','王二','李七'])
print(series1)
#output
张三 70
王二 82
李七 68
dtype: int64
print(series1.index)
#output:
RangeIndex(start=0, stop=20, step=1)
print(series.values)
#output:
[70 82 68]
print(series.dtype)
#output:
int64
**关于为何上面是int32 这里是int64的问题还没查清楚。。。
(3)缺失值的检测 isnull()与notnull()
Series2=Series([1,2,3,4,np.NaN,5,6,7,8])
print(Series2)
#output:
0 1.0
1 2.0
2 3.0
3 4.0
4 NaN
5 5.0
6 6.0
7 7.0
8 8.0
dtype: float64
print(Series2.isnull())
#output:
0 False
1 False
2 False
3 False
4 True
5 False
6 False
7 False
8 False
dtype: bool
print(Series2.notnull())
output:
0 True
1 True
2 True
3 True
4 False
5 True
6 True
7 True
8 True
dtype: bool
print(Series2[pd.isnull(Series2)]) #过滤出为缺失值的项 #只是显示 不做替换
#output:
4 NaN
dtype: float64
# print(Series2[pd.notnull(Series2)])#过滤出不是缺失值的项 #只是显示 不做替换
#output:
0 1.0
1 2.0
2 3.0
3 4.0
5 5.0
6 6.0
7 7.0
8 8.0
dtype: float64
(4)Series自动对齐
不同Series之间进行算术运算,会自动对齐不同索引的数据(将索引排序,对应相乘)
product_num=Series([23,3,56,78],index=['p3','p2','p4','p5'])
product_price=Series([13,24,5,2,1],index=['p1','p2','p3','p4','p5'])
product=product_num*product_price
print(product)
#output
p1 NaN
p2 72.0
p3 115.0
p4 112.0
p5 78.0
dtype: float64
(5)通过索引从Series中取值:类似于切片
series=Series([34,56,78,12],index=['1','2','3','4'])
print(series[2]) #78
print(series['2']) #56
#注意带引号与不带引号得到值不同
print(series['1':'3'])
#类似于切片,但是边界右边是包含的,这与python中基础列表不一样
#output
1 34
2 56
3 78
dtype: int64
print(series[1:3])
#output
2 56
3 78
dtype: int64
#注意:当索引里的值加引号时,得到结果是都包含,当不加引号时,结果是前闭后开(默认从0开始下标)
print (series['1':])#从索引为1值取到结尾
#output
1 34
2 56
3 78
4 12
dtype: int64
print(series[:'3'])#从开头取到索引为3对应值
#output
1 34
2 56
3 78
dtype: int64
(6)修改索引对应值
series['1']=66
print(series['1'])
#output 66
series[:'2']=[1,3]
print(series)
#output
1 1
2 3
3 78
4 12
dtype: int64
(7)指定行名与列名
#指定行名与列名
df1=DataFrame({'m':[1,2,3],'n':[4,5,6]},index=['a','b','c'])
#output
m n
a 1 4
b 2 5
c 3 6
#指定列名
df2=DataFrame({'m':[1,2,3],'n':[4,5,6]})
#output
m n
0 1 4
1 2 5
2 3 6
#指定行名
df=DataFrame([[1,2,3],[4,5,6]],index=['a','b'])
#output
0 1 2
a 1 2 3
b 4 5 6
print(df2[1]['a']) #取出第二列第a行的数据 列在前面 注意行列先后顺序 先列后行
#output 2
print(df2[1:]) #如果使用切片,那么从行开始取值
#output
0 1 2
b 4 5 6
方法二定义行标题与列标题
arr=np.array([['Tom',76],['Merry',98],['Jim',100]])
df3=DataFrame(arr,index=['one','two','three'],columns=['name','score'])
#output
name score
one Tom 76
two Merry 98
three Jim 100
四、pandas基本功能
(1)重新索引
df3.index=['1','2','3']
#output
name score
1 Tom 76
2 Merry 98
3 Jim 100
df3.columns=['honey','lik']
#output
honey lik
1 Tom 76
2 Merry 98
3 Jim 100
(2)新定义一个数据框
n1=np.random.random((20,6)) #20行6列的随机数
s1=DataFrame(n1)
#output
0 1 2 3 4 5
0 0.296291 0.611742 0.272734 0.365260 0.274213 0.953953
1 0.927341 0.717047 0.777421 0.337505 0.961485 0.553750
2 0.220768 0.758849 0.912330 0.492096 0.849173 0.362099
3 0.551228 0.703351 0.229545 0.054826 0.629826 0.883706
4 0.223971 0.780404 0.881662 0.870573 0.447324 0.436546
5 0.748838 0.752578 0.853491 0.595867 0.609587 0.768815
6 0.137102 0.445860 0.357145 0.009206 0.487706 0.500765
7 0.818974 0.452439 0.582092 0.490219 0.579992 0.414877
8 0.880320 0.231642 0.872773 0.206397 0.987318 0.293588
9 0.436617 0.097572 0.998811 0.668974 0.209691 0.826550
10 0.537315 0.896117 0.944000 0.747639 0.871928 0.516713
11 0.122376 0.955835 0.569449 0.273230 0.348618 0.359314
12 0.147378 0.825629 0.479179 0.740715 0.082364 0.802855
13 0.749187 0.981832 0.532099 0.097261 0.439028 0.204709
14 0.302841 0.542556 0.440156 0.228613 0.508635 0.562280
15 0.074067 0.617504 0.160624 0.250712 0.367737 0.430470
16 0.216264 0.537888 0.625030 0.685654 0.190983 0.835050
17 0.103122 0.374838 0.577580 0.913814 0.512975 0.859101
18 0.237264 0.976264 0.257229 0.957545 0.734126 0.559889
19 0.456728 0.617536 0.993404 0.252058 0.884959 0.883279
#头部数据
print(s1.head(1)) # 不带参数的时候默认前五
#output
0 1 2 3 4 5
0 0.138761 0.915466 0.671729 0.981149 0.010045 0.532475
#尾部数据
print(s1.tail(1)) # 不带参数的时候默认后5
0 1 2 3 4 5
19 0.492207 0.710456 0.965255 0.335632 0.941908 0.876229
(3)数据快速统计
print(s1.describe())
#output
0 1 2 3 4 5
count 20.000000 20.000000 20.000000 20.000000 20.000000 20.000000
mean 0.457349 0.363549 0.534867 0.602121 0.498337 0.393965
std 0.273798 0.316593 0.345369 0.256599 0.265528 0.258933
min 0.022351 0.013766 0.019992 0.010644 0.028562 0.021790
25% 0.250253 0.075698 0.188173 0.463704 0.290076 0.231641
50% 0.505490 0.321201 0.548391 0.617214 0.488301 0.363083
75% 0.657500 0.521612 0.869519 0.763534 0.706839 0.492462
max 0.887665 0.959794 0.943535 0.983154 0.980503 0.963295
#转置
print(s1.T)
#按轴排序
# print(s1.sort_index(axis=1,ascending=False)) #0代表行轴,1代表列轴,表示按照索引排序,内容跟着索引走
#按值排序
# print(s1.sort_values(by=[2],ascending=True)) #代表按行排序,倒序
# print(s1.sort_values(by=[2,4],ascending=[True,False]))#代表第二行按照升序第四行按照降序
(4)按照标签获取交叉区域的值
print(s1.loc[[1,15,9]]) #取指定某几行
#output
0 1 2 3 4 5
1 0.665831 0.231751 0.330863 0.077317 0.693127 0.087671
15 0.390360 0.150199 0.645565 0.868839 0.586098 0.724746
9 0.611332 0.833997 0.395407 0.782962 0.947267 0.014603
print(s1.loc[0:5,[3]]) #取0到5行中的列索引为3的值
#s1.loc[0,1] 与s1[1][0]等价