【前情提要】为什么要学习pandas
because
(1)numpy能够帮助我们处理数据,能够结合matplotlib解决数据分析的问题,那么pandas学习的目的是什么呢?
(2)numpy主要解决数值型数据。
(3)而数据除了数值外,还有字符串,还有时间序列等.
(4)比如:我们通过爬虫获取到了存储在数据库中的数据
(5)比如:之前YouTube的例子中除了数值外还有国家的信息,视频的分类(tag)信息,标题信息等
so
(1)所以,numpy能够帮助我们处理数值,但是pandas除了处理数值之外(基于numpy),还能够帮助我们处理其他类型的数据
注意:
(1)Series --- 一维数据,带标签数组
(2)DataFrame --- 二维数据,Series容器
【问题1】pandas之Series创建
import pandas as pd
# (1)
t1 = pd.Series([1,2,31,12,3,4])print(t1)print(type(t1))print('**(1)**'*10)# (2) 添加索引 index
t2 = pd.Series([1,2,31,12,3,4],index=list('abcdef'))# 将字符串转换为列表类型 ---- list( )print(t2)print(t2.dtype)# t2----整数类型 ---- dtype: int64print(t2.astype('float'))# 指定类型为floatprint('**(2)**'*10)# (3)字典:键--->索引 值--->值
temp_dict ={'name':'xiaohong','age':30,'tel':10086}
t3 = pd.Series(temp_dict)print(t3)print(t3.dtype)# t3----字符串类型 ---- dtype: objectprint('**(3)**'*10)# (4)import string
t1 = string.ascii_uppercase[:10]print(t1)
t2 =list(string.ascii_uppercase[:10])print(t2)
t3 = pd.Series(np.arange(10),index=list(string.ascii_uppercase[:10]))print(t3)print(type(t3))print(t3.dtype)print('**(4)**'*10)# (5)'''
(1)通过字典创建一个Series,注意其中的索引就是字典的键
(2)重新给其指定的其他索引后,如果能够对应上,就取其值;如果不能,就为nan
(3)numpy中nan为float,pandas会自动根据数据类更改Series的dtype类型
'''import string
t ={string.ascii_uppercase[i]:i for i inrange(10)}# 创建字典print(t)
t1 = pd.Series(t)print(t1)
t2 = pd.Series(t,index=list(string.ascii_uppercase[5:15]))# 取索引: [5,15)print(t2)print('**(5)**'*10)
0 1
1 2
2 31
3 12
4 3
5 4
dtype: int64
<class 'pandas.core.series.Series'>
**(1)****(1)****(1)****(1)****(1)****(1)****(1)****(1)****(1)****(1)**
a 1
b 2
c 31
d 12
e 3
f 4
dtype: int64
int64
a 1.0
b 2.0
c 31.0
d 12.0
e 3.0
f 4.0
dtype: float64
**(2)****(2)****(2)****(2)****(2)****(2)****(2)****(2)****(2)****(2)**
name xiaohong
age 30
tel 10086
dtype: object
object
**(3)****(3)****(3)****(3)****(3)****(3)****(3)****(3)****(3)****(3)**
ABCDEFGHIJ
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
A 0
B 1
C 2
D 3
E 4
F 5
G 6
H 7
I 8
J 9
dtype: int32
<class 'pandas.core.series.Series'>
int32
**(4)****(4)****(4)****(4)****(4)****(4)****(4)****(4)****(4)****(4)**
{'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9}
A 0
B 1
C 2
D 3
E 4
F 5
G 6
H 7
I 8
J 9
dtype: int64
F 5.0
G 6.0
H 7.0
I 8.0
J 9.0
K NaN
L NaN
M NaN
N NaN
O NaN
dtype: float64
**(5)****(5)****(5)****(5)****(5)****(5)****(5)****(5)****(5)****(5)**
{'age': 30, 'name': 'xiaohong', 'tel': 10086}
age 30
name xiaohong
tel 10086
dtype: object
30
10086
******************************
30
xiaohong
10086
******************************
age 30
name xiaohong
dtype: object
name xiaohong
tel 10086
dtype: object
**(1)****(1)****(1)****(1)****(1)****(1)****(1)****(1)****(1)****(1)**
0 1
1 2
2 31
3 12
4 3
5 4
dtype: int64
2 31
3 12
dtype: int64
**(2)****(2)****(2)****(2)****(2)****(2)****(2)****(2)****(2)****(2)**
age 30
name xiaohong
tel 10086
dtype: object
Index(['age', 'name', 'tel'], dtype='object')
<class 'pandas.core.indexes.base.Index'>
3
['age', 'name', 'tel']
['age', 'name']
age
name
tel
******************************
[30 'xiaohong' 10086]
<class 'numpy.ndarray'>
**(3)****(3)****(3)****(3)****(3)****(3)****(3)****(3)****(3)****(3)**
0 0
1 1
2 2
3 3
4 4
dtype: int64
0 NaN
1 1.0
2 2.0
3 3.0
4 4.0
dtype: float64
0 10
1 10
2 2
3 3
4 4
dtype: int64
**(4)****(4)****(4)****(4)****(4)****(4)****(4)****(4)****(4)****(4)**
A 0
B 1
C 2
D 3
E 4
F 5
G 6
H 7
I 8
J 9
dtype: int64
C 2
E 4
G 6
I 8
dtype: int64
1
C 2
D 3
G 6
dtype: int64
F 5
G 6
H 7
I 8
J 9
dtype: int64
5
A 0
F 5
G 6
dtype: int64
**(5)****(5)****(5)****(5)****(5)****(5)****(5)****(5)****(5)****(5)**