Python数据分析-Pandas(Series与DataFrame)

Pandas介绍:

  pandas是一个强大的Python数据分析的工具包,是基于NumPy构建的。

Pandas的主要功能:
  1)具备对其功能的数据结构DataFrame、Series
  2)集成时间序列功能
  3)提供丰富的数学运算和操作
  4)灵活处理缺失数据

pyhton里面安装、引入方式:
  安装方法:pip install pandas
  引用方法:import pandas as pd

pands的数组的创建:

创建空的的值

import pandas as pd
s = pd.Series()
print(s)  #Series([], dtype: float64)

传入一个列表

data=['a','b','c','d']
res=pd.Series(data)
print(res)

'''结果
0    a
1    b
2    c
3    d
这里没有传递任何索引,因此默认情况下,它分配了从0到len(data)-1的索引,即:0到3
'''

传一个字典

data = {'a' : 0, 'b' : 1, 'c' : 2}
s = pd.Series(data)
print(s)
'''结果
a    0
b    1
c    2
dtype: int64

注意 - 字典键用于构建索引。

'''

从标量创建索引:

如果数据是标量值,则必须提供索引。将按照索引重复该值进行匹配

res=pd.Series(0, index=['a','b','c','d'])
print(res)

'''结果
a    0
b    0
c    0
d    0

'''

自指定索引值:

res=pd.Series(['a','b','c','d'],index=['a_index','b_index','c_index','d_index'])
print(res)

'''结果
a_index    a
b_index    b
c_index    c
d_index    d

'''

从具有位置的系列中访问数据(取值):

重点理解:数组是从零开始计数的,第一个位置存储再零位置)

查看index 、 values的值:

#查看数组的index值
print(res.index)

#查看数组的value值
print(res.values)


#取值(根据默认第零位开始取)
print(res[0])  #a

取前三个值(不包括定义的最后一个数值)

res=pd.Series(['a','b','c','d'],index=['a_index','b_index','c_index','d_index'])

#取前三个值(不包括3)
print(res[:3]) #是个对象可以 res[:3].values
'''结果

  a_index a
  b_index b
  c_index c
  dtype: object

'''

取后三个值:

print(res[-3:])

'''结果
b_index    b
c_index    c
d_index    d
dtype: object

'''

使用索引标签检索数据并设置数据:

修改value值

res=pd.Series(['a','b','c','d'],index=['a_index','b_index','c_index','d_index'])
print(res)
res['a_index']='new_a'
print(res)

'''结果

a_index    new_a
b_index        b
c_index        c
d_index        d

'''

copy复制数据并修改

sr1=pd.Series([12,13,14],index=['c','a','d'])
sr2=pd.Series([14,15,16],index=['d','c','a'])

#可以使用copy赋值数组再修改
sr3=sr1[1:].copy()
print(sr3)

sr3[0]=1888
print(sr3)

'''
a    13
d    14
dtype: int64

a    1888
d      14
dtype: int64
'''

运算:

初始构建2个数组

sr1=pd.Series([12,13,14],index=['c','a','d'])

sr2=pd.Series([14,15,16],index=['d','c','a'])

print(sr1+sr2)
'''结果
a    29
c    27
d    28


''' 

求和运算

Pandas自动对齐功能,如果自定义了索引就会找原来索引,如果没有值就为NaN

sr1=pd.Series([12,13,14],index=['c','a','d'])
sr3=pd.Series([11,20,10,14], index=['d','c','a','b'])
print(sr3)
#求sr1+sr3和值
print(sr1+sr3)
'''结果

a    23.0
b     NaN  #一位sr1中没有索引b,所以显示空
c    32.0
d    25.0

Pandas自动对齐功能,如果自定义了索引就会找原来索引,如果没有值就为NaN

'''

NaN缺失数据的操作:

#先构建一个缺失数据
sr1=pd.Series([12,13,14],index=['c','a','d'])
sr2=pd.Series([14,15,16],index=['d','c','a'])

sr3=pd.Series([11,20,10,14], index=['d','c','a','b'])

#合并生成一个缺失数据
sr4=sr1+sr3
print(sr4)

'''结果

a    23.0
b     NaN
c    32.0
d    25.0
dtype: float64

'''

isnull,返回布尔数组,缺失值对应True

#isnull,返回布尔数组,缺失值对应True
res=pd.isnull(sr4)
print(res)

'''结果
a    False
b     True
c    False
d    False

'''

notnull,返回布尔数组,缺失值对应为False

#notnull,返回布尔数组,缺失值对应为False
res=pd.notnull(sr4)
print(res)
'''结果
a     True
b    False
c     True
d     True
dtype: bool

'''

dropna,过滤掉有NaN的行

#dropna,过滤掉有NaN的行
res=pd.Series.dropna(sr4)
print(res)

'''
a    23.0
c    32.0
d    25.0
dtype: float64

'''

fillna,填充缺失的数据

#fillna,填充NaN缺失的数据
res=sr4.fillna('这是给NaN做填充的数据')
print(res)

'''数据结构
a              23
b    这是给NaN做填充的数据
c              32
d              25
dtype: object

'''

DataFrame分析,待完善。。

猜你喜欢

转载自www.cnblogs.com/yangzhizong/p/10074583.html