- Python Data Analysis Library或pandas是基于NumPy的一种工具,该工具是为了解决数据分析任务而创建的
- Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具
- Pandas 提供了大量能使我们快速便捷地处理数据的函数和方法
- 它使Python成为强大而高效的数据分析环境的重要因素之一
-
可以理解为带标签的NumPy数组
-
Series是一个类似一维数组的数据结构
-
DataFrame数据帧,类似于Excel,DateFrame组织数据,处理数据
-
Pandas 是数据分析的重要模块
- 用于数据的清洗,数据的处理,数据的统计分析,数据的计算
- Pandas里面重要的两个类:Series,DataFrame
- Series 是带标签的一维数组
- DataFrame是带标签的二维数组,即由多个Series组成的关系表(数据帧)
- Pandas是依赖numpy库
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
- 基于list或ndarray创建
- 基于dict方式创建
- Series(data,index,dtype,name)
- data 表示数据,可以是ndarray,list,tuple,dict
- index 表示索引标签,个数同data数量保持一致
- name 表示Series对象的名称
- list列表方式来创建
course = ['语文','Math','英语','Physics','Chemistry','History']
score = [80,72,65,55,26,38]
person1 = Series(data=score,index=course,dtype=float,name="小明")
person1
- ndarray数组的方式创建
person2 = Series(data=score,index=course,dtype=int,name="小强")
person2
[out]:
语文 80
Math 72
英语 65
Physics 55
Chemistry 26
History 38
Name: 小强, dtype: int32
[int]:
person3 = Series(np.random.randint(100,size=6),index=course,dtype=int,name="小龙")
person3
[out]:
语文 10
Math 43
英语 98
Physics 59
Chemistry 85
History 58
Name: 小龙, dtype: int32
- dict方式创建
- [注意] key即是索引标签,不需要指定index标签的列表或数组
person4 = Series({
'语文':80,
'数学':93,
'英语':54,
'化学':32,
'物理':66
},dtype=int,name="小王")
person4
[out]:
语文 80
数学 93
英语 54
化学 32
物理 66
Name: 小王, dtype: int32
- 显示索引,即索引标签
- s[‘标签名’]
- s.loc[‘标签名’]
- s.loc[‘开始标签’:’结束标签’]
- s[[‘标签1′,’标签2’]]
- s.loc[[‘标签1′,’标签2’]]
- 隐式索引,即索引下标
- s.iloc[索引下标]
- s.iloc[开始下标:结束下标]
- s.iloc[[下标1,下标2]]
person1
[out]:
语文 80.0
Math 72.0
英语 65.0
Physics 55.0
Chemistry 26.0
History 38.0
Name: 小明, dtype: float64
[int]:
person1['History']
[out]:
38.0
[int]:
person1[['语文','Math','英语']]
[out]:
语文 80.0
Math 72.0
英语 65.0
Name: 小明, dtype: float64
[int]:
person1[['语文','Physics','History']]
[out]:
语文 80.0
Physics 55.0
History 38.0
Name: 小明, dtype: float64
[int]:
person1['Math':'Chemistry']
[out]:
Math 72.0
英语 65.0
Physics 55.0
Chemistry 26.0
Name: 小明, dtype: float64
[int]:
person1.loc['Math':'Chemistry']
[out]:
Math 72.0
英语 65.0
Physics 55.0
Chemistry 26.0
Name: 小明, dtype: float64
[int]:
person3
[out]:
语文 10
Math 43
英语 98
Physics 59
Chemistry 85
History 58
Name: 小龙, dtype: int32
[int]:
person3['Physics']=38
person3
[out]:
语文 10
Math 43
英语 98
Physics 38
Chemistry 85
History 58
Name: 小龙, dtype: int32
[int]:
person3 +=5
person3
[out]:
语文 20
Math 53
英语 108
Physics 48
Chemistry 95
History 68
Name: 小龙, dtype: int32
[int]:
person3 /=2
person3
[out]:
语文 5.00
Math 13.25
英语 27.00
Physics 12.00
Chemistry 23.75
History 17.00
Name: 小龙, dtype: float64
[int]:
person2
[out]:
语文 80
Math 72
英语 65
Physics 55
Chemistry 26
History 38
Name: 小强, dtype: int32
[int]:
person2.iloc[-3:] +=30
person2
[out]:
语文 80
Math 72
英语 65
Physics 115
Chemistry 86
History 98
Name: 小强, dtype: int32
[int]:
person2.iloc[:2]
[out]:
语文 80
Math 72
Name: 小强, dtype: int32
[int]:
person3
[out]:
语文 105.00
Math 13.25
英语 27.00
Physics 12.00
Chemistry 23.75
History 117.00
Name: 小龙, dtype: float64
[int]:
person3.iloc[[0,-1]] -= 50
person3
[out]:
语文 55.00
Math 13.25
英语 27.00
Physics 12.00
Chemistry 23.75
History 67.00
Name: 小龙, dtype: float64
- 包含了ndarray对象的属性
- name 名称
- index 索引标签
- values 是ndarray数组内容
person3.values
[out]:
array([55. , 13.25, 27. , 12. , 23.75, 67. ])
[int]:
person3.name
[out]:
'小龙'
[int]:
person3.index
[out]:
Index(['语文', 'Math', '英语', 'Physics', 'Chemistry', 'History'], dtype='object')
- not a number
person4
[out]:
语文 80
数学 93
英语 54
化学 32
物理 66
Name: 小王, dtype: int32
[int]:
person4['英语'] = np.nan
person4
[out]:
[int]:
person4 + 10
person4
[out]:
- s.add() 相加
- s.sub() 相减
- s.divide()/s.div() 相除
- s.mod() 取余
- s.multiply() 乘法
person4
[out]:
[int]:
person4 = person4.add(10,fill_value=0)
person4
[out]:
语文 100.0
数学 113.0
英语 20.0
化学 52.0
物理 86.0
Name: 小王, dtype: float64
[int]:
person4["化学"]= np.nan
person4
[out]:
[int]:
person4 =person4.sub(10,fill_value=20)
person4
[out]:
[int]:
person4.divide(2)
[out]:
[int]:
person4
[out]:
语文 110.0
数学 123.0
英语 30.0
化学 70.0
物理 96.0
Name: 小王, dtype: float64
[int]:
person4.mod(10)
[out]:
语文 0.0
数学 3.0
英语 0.0
化学 0.0
物理 6.0
Name: 小王, dtype: float64
[int]:
person4
[out]:
语文 110.0
数学 123.0
英语 30.0
化学 70.0
物理 96.0
Name: 小王, dtype: float64
[int]:
person4.multiply(2)
[out]:
语文 220.0
数学 246.0
英语 60.0
化学 140.0
物理 192.0
Name: 小王, dtype: float64
Original: https://blog.csdn.net/m0_57021623/article/details/123996551
Author: 今晚务必早点睡
Title: 数据分析——从入门到精通(三)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/675665/
转载文章受原作者版权保护。转载请注明原作者出处!