数据分析工具 Pandas第一部分

本文介绍了Pandas库中Series和Dataframe的基本概念、创建方法、索引与切片、数据查看与修改、以及高级技巧如对齐、排序和布尔型索引。涵盖了数据结构的创建、操作和管理,适合数据分析初学者学习。
摘要由CSDN通过智能技术生成

引言

  • pandas - Python Data Analysis Library

pandas is a fast, powerful, flexible and easy to use open source data analysis
and manipulation tool,built on top of the Python programming language.
Pandas是一个快速、强大、灵活、易用的开源数据分析软件
以及建立在Python编程语言之上的操作工具包。

Pandas数据结构Series

【课程2.2】 Series:基本概念及创建

  • 带有标签的一维数组:Series
  • 可以保存任何数据类型(整数,字符串,浮点数,Python对象等)
  • 轴标签统称为索引
# 导入numpy、pandas模块
import numpy as np
import pandas as pd  

s = pd.Series(np.random.rand(5))
print(s)
# 查看数据、数据类型
print('查看数据、数据类型:',type(s))


print(s.index,type(s.index))
print(s.values,type(s.values))
# .index查看series索引,类型为rangeindex
# .values查看series值,类型是ndarray
0    0.681858
1    0.070453
2    0.839162
3    0.841539
4    0.281292
dtype: float64
查看数据、数据类型: <class 'pandas.core.series.Series'>
RangeIndex(start=0, stop=5, step=1) <class 'pandas.core.indexes.range.RangeIndex'>
[0.68185769 0.07045322 0.83916236 0.84153898 0.28129169] <class 'numpy.ndarray'>
# 核心:series相比于ndarray,是一个自带索引index的数组 → 一维数组 + 对应索引
# 所以当只看series的值的时候,就是一个ndarray
# series和ndarray较相似,索引切片功能差别不大
# series和dict相比,series更像一个有顺序的字典(dict本身不存在顺序),其索引原理与字典相似(一个用key,一个用index)

Series 创建

1、由字典创建
2、由数组或列表创建
3、由标量创建
# Series 创建方法一:由字典创建,字典的key就是index,values就是values

dic = {
   'a':1 ,'b':2 , 'c':3, '4':4, '5':5}
s = pd.Series(dic)
print(s)
# 注意:key肯定是字符串,假如values类型不止一个会怎么样? → dic = {'a':1 ,'b':'hello' , 'c':3, '4':4, '5':5}
a    1
b    2
c    3
4    4
5    5
dtype: int64
# Series 创建方法二:由数组创建(一维数组)

arr = np.random.randn(5)
s = pd.Series(arr)
print(arr)
print(s)
# 默认index是从0开始,步长为1的数字

s = pd.Series(arr, index = ['a','b','c','d','e'],dtype = np.object)
print(s)
# index参数:设置index,长度保持一致
# dtype参数:设置数值类型
[-1.00050265  1.5435296   0.99945534  2.03576577  0.3701096 ]
0   -1.000503
1    1.543530
2    0.999455
3    2.035766
4    0.370110
dtype: float64
a   -1.000503
b     1.54353
c    0.999455
d    2.035766
e     0.37011
dtype: object


<ipython-input-4-32e49dec4503>:9: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  s = pd.Series(arr, index = ['a','b','c','d','e'],dtype = np.object)
# Series 创建方法三:由标量创建

s = pd.Series(10, index = range(4))
print(s)
# 如果data是标量值,则必须提供索引。该值会重复,来匹配索引的长度
0    10
1    10
2    10
3    10
dtype: int64
# Series 名称属性:name

s1 = pd.Series(np.random.randn(5))
print(s1)
print('-----')
s2 = pd.Series(np.random.randn(5),name = 'test')
print(s2)
print('s1.name:',s1.name, '\ns2.name:',s2.name,type(s2.name))
# name为Series的一个参数,创建一个数组的 名称
# .name方法:输出数组的名称,输出格式为str,如果没用定义输出名称,输出为None

s3 = s2.rename('hehehe')
print(s3)
print(s3.name, s2.name)
print("s3 is s2??",s3 is s2)# 名字不同,对象也非同一个
# .rename()重命名一个数组的名称,并且新指向一个数组,原数组不变
0    1.218290
1    1.474118
2    1.354741
3    0.386391
4   -0.528123
dtype: float64
-----
0    0.963783
1   -0.481901
2   -0.985653
3    1.282279
4    0.151822
Name: test, dtype: float64
s1.name: None 
s2.name: test <class 'str'>
0    0.963783
1   -0.481901
2   -0.985653
3    1.282279
4    0.151822
Name: hehehe, dtype: float64
hehehe test
s3 is s2?? False

######## 本节课有作业,请查看 “课程作业.docx” ########

【课程2.3】 Pandas数据结构Series:索引与切片

位置下标
标签索引
切片索引
布尔型索引
# 位置下标,类似序列

s = pd.Series(np.random.rand(5))
print(s)
print(s[0],type(s[0]),s[0].dtype)
print(float(s[0]),type(float(s[0])))
#print(s[-1])
# 位置下标从0开始
# 输出结果为numpy.float格式,
# 可以通过float()函数转换为python float格式
# numpy.float与float占用字节不同
# s[-1]结果如何?
0    0.537268
1    0.307512
2    0.259983
3    0.101515
4    0.267092
dtype: float64
0.5372683484875763 <class 'numpy.float64'> float64
0.5372683484875763 <class 'float'>
# 标签索引

s = pd.Series(np.random.rand(5), index = ['a','b','c','d','e'])
print(s)
print(s['a'],type(s['a']),s['a'].dtype)
# 方法类似下标索引,用[]表示,内写上index,注意index是字符串

sci = s[['a','b','e']]
print(sci,type(sci))
# 如果需要选择多个标签的值,用[[]]来表示(相当于[]中包含一个列表)
# 多标签索引结果是新的数组
a    0.853171
b    0.900980
c    0.078312
d    0.414482
e    0.930324
dtype: float64
0.8531710837483069 <class 'numpy.float64'> float64
a    0.853171
b    0.900980
e    0.930324
dtype: float64 <class 'pandas.core.series.Series'>
# 切片索引

s1 = pd.Series(np.random.rand(5))
s2 = pd.Series(np.random.rand(5), index = ['a','b','c','d','e'])
print(s1[1:4],s1[4]) # 数字做索引切片末端不包含
print(s2['a':'c'],s2['c'])
print(s2[0:3],s2[3])
print('-----')
# 注意:用index做切片是末端包含

print(s2[:-1])
print(s2[::2])
# 下标索引做切片,和list写法一样
1    0.664878
2    0.425142
3    0.666980
dtype: float64 0.24234658608411197
a    0.431778
b    0.347889
c    0.320653
dtype: float64 0.3206529401888253
a    0.431778
b    0.347889
c    0.320653
dtype: float64 0.04409958140998871
-----
a    0.431778
b    0.347889
c    0.320653
d    0.044100
dtype: float64
a    0.431778
c    0.320653
e    0.378013
dtype: float64
# 布尔型索引

s = pd.Series(np.random.rand(3)*100)
s[4] = None  # 添加一个空值
print(s)
bs1 = s > 50
bs2 = s.isnull()
bs3 = s.notnull()
print(bs1, type(bs1), bs1.dtype)
print(bs2, type(bs2), bs2.dtype)
print(bs3, type(bs3), bs3.dtype)
print('-----')
# 数组做判断之后,返回的是一个由布尔值组成的新的数组
# .isnull() / .notnull() 判断是否为空值 (None代表空值,NaN代表有问题的数值,两个都会识别为空值)

print(s[s > 50])
print(s[bs3])
# 布尔型索引方法:用[判断条件]表示,其中判断条件可以是 一个语句,或者是 一个布尔型数组!
0    42.191458
1    41.566278
2    35.940391
4         None
dtype: object
0    False
1    False
2    False
4    False
dtype: bool <class 'pandas.core.series.Series'> bool
0    False
1    False
2    False
4     True
dtype: bool <class 'pandas.core.series.Series'> bool
0     True
1     True
2     True
4    False
dtype: bool <class 'pandas.core.series.Series'> bool
-----
Series([], dtype: object)
0    42.191458
1    41.566278
2    35.940391
dtype: object

######## 本节课有作业,请查看 “课程作业.docx” ########

se=pd.Series(np.random.randint(0,100,size=(10,)),index=[chr(x) for x in range(97,107)])
print(se[['b',
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值