Python数据分析学习笔记—Pandas基础知识

一、基本概念

​ pandas 是基于NumPy的一种工具,该工具是为了解决数据分析任务而创建的。Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。pandas提供了大量能使我们快速便捷地处理数据的函数和方法。你很快就会发现,它是使Python成为强大而高效的数据分析环境的重要因素之一

二、常见的数据结构
1. Series

一维数组,与Numpy中的一维array类似。二者与Python基本的数据结构List也很相近。Series如今能保存不同种数据类型,字符串、boolean值、数字等都能保存在Series中

2. DataFrame

二维的表格型数据结构。很多功能与R中的data.frame类似。可以将DataFrame理解为Series的容器。

三、库的导入
库名:pandas库
别名:pd
import pandas as pd #直接调用pd方法
from pandas import Series,DataFrame #直接使用Series,DataFrame方法来创建,可以省略
四、Series
类型:类似于ndarray类型
行索引:index
索引对应的数据:values
主要包含:Series的创建、Series的操作方法、Series的常用方法、Series的算数运算
1、Series的创建
小编思路:
` 创建方法有三种:列表,字典,numpy
# 库的导入
import pandas as pd
from pandas import Series,DataFrame
import numpy as np
1.1 由列表创建
Series(data=[1,2,3],index=['a','b','c'])
#另一种写法   pd.Series(data=[1,2,3],index=['a','b','c']),下面也是可以使用这种方法
————————————————
a    1
b    2
c    3
dtype: int64
1.2 由字典创建
Series(data={'1':2,'2':3})
————————————————
1    2
2    3
dtype: int64
1.3 由numpy创建
Series(data=np.random.randint(1,10,6))
————————————————
0    9
1    1
2    3
3    4
4    3
5    4
dtype: int32
2、Series的操作:索引和切片
小编思路:

`显式索引:自己直接规定的index(比如下面创建Series:[‘a’,'b','c'])

`隐式索引:系统分配的index([1,2,3])

注意:自己设置的索引不会覆盖隐形索引

s = Series(data=[1,2,3],index=['a','b','c'])
-------------
a    1
b    2
c    3
dtype: int64
2.1 索引
s['a']
s[0]
---------
1
1
2.2 切片
s[0:2]
----------
a    1
b    2
dtype: int64
s[0:3:2]
----------
a    1
c    3
dtype: int64
2.3 返回索引
s.index
---------
Index(['a', 'b', 'c'], dtype='object')
2.4 返回索引值
s.values
---------
array([1, 2, 3], dtype=int64)
3、Series的常用方法
小编思路:
`三种常用方法:数据显示,数据去重,数据空值检验
3.1 显示前2个元素
s.head(2)
-----------
a    1
b    2
dtype: int64
3.2 显示后2个元素
s.tail(2)
-------------
b    2
c    3
dtype: int64
3.3 数据去重
s1 =Series(data=[1,1,1,23,44,34,23])
s1.unique()
------------------------------
array([ 1, 23, 44, 34], dtype=int64)
3.4 数据去重之后返回的个数
s1.nunique()
----------------
4
3.5 检查元素是否为空
s1.isnull()
-----------
0    False
1    False
2    False
3    False
4    False
5    False
6    False
dtype: bool
3.6 检查元素是否为非空
s1.notnull()
-------------
0    True
1    True
2    True
3    True
4    True
5    True
6    True
dtype: bool
4、Series的算术运算
s1 = Series(data=[1,2,3],index=['a','b','c'])
s2 = Series(data=[1,2,3],index=['a','c','d'])
s1+s2
---------------
a    2.0
b    NaN
c    5.0
d    NaN
dtype: float64
五、DataFrame
表格类型的数据(二维的数据)
index行索引;columns列索引;values值
主要包含:DataFrame创建方法,DataFrame的属性,DataFrame的索引和切片,DataFrame的运算
1、DataFrame创建方法
小编思路:
`创建方法:二维列表方式,字典方式,numpy,
1.1 以二维列表方式创建
DataFrame(data=[[1,2,3],[4,5,6]],index=['1','0'])
# pd.DataFrame(data=[[1,2,3],[4,5,6]],index=['1','0'])是另一种方式
--------------------------------------
012
1123
0456
1.2 以字典方式创建
dic = {
    '1':'la',
    '2':'lq',
    '3':'lw'}
DataFrame(dic,index=['名字'])
------------------------------------------------
123
名字lalqlw
1.3 numpy创建
df = DataFrame(data=np.random.randint(0,100,(3,2)),index=['a','b','c'],columns=['A','B'])
df
---------------------------------------
AB
a723
b9826
c415
2、DataFrame的属性
小编思路:
`属性:值,索引,返回类型
2.1 返回值
df.values
--------------
array([[72,  3],
       [98, 26],
       [41,  5]])
2.2返回行索引
df.index
---------------
Index(['a', 'b', 'c'], dtype='object')
2.3返回列索引
df.columns
----------------
Index(['A', 'B'], dtype='object')
2.4返回类型
df.shape
----------------
(3, 2)
3、DataFrame的索引
小编思路:
`对行、列进行索引,使用索引取出元素
da= DataFrame(data=[[150,0],[150,0],[150,0],[300,0]],index=['语文','数学','英语','理科'],columns=['张三','李四'])
da
张三李四
语文1500
数学1500
英语1500
理科3000
3.1 对列进行索引
#对列进行索引(显性)
da['张三']
#对列进行索引(隐性)
da.iloc[:,0]
同样结果------------------------
语文    150
数学    150
英语    150
理科    300
Name: 张三, dtype: int64
3.2 对行进行索引
#对行进行索引(显式)
da.loc['语文']
##对行进行索引(隐式)
da.iloc[0]
同样结果------------------
张三    150
李四      0
Name: 语文, dtype: int64
3.3 取元素
#取元素(显式)
da.loc['语文','张三']
--------------------
150
#取元素(隐式)
da.iloc[0,0]
--------------------
150
#取多个元素(隐式)
da.iloc[[0,1],0]
-----------------------
语文    150
数学    150
Name: 张三, dtype: int64
4、DataFrame的切片
小编思路:
`对行、列进行切片
4.1 切行
da[0:2]
----------------------------
张三李四
语文1500
数学1500
4.2 切列
da.iloc[:,0:1]
------------------------
张三
语文150
数学150
英语150
理科300
5、DataFrame的运算
ddd = {
  'job':[94,90,90,91],
  'bob':[65,56,86,87],
  'li':[87,98,98,78]
}
dd = DataFrame(ddd,index=['语文','数学','英语','体育'])
dd
-------------------------------------------------------
jobbobli
语文946587
数学905698
英语908698
体育918778
ddf = {
  'job':[84,98,90,91],
  'bob':[65,89,86,87],
  'li':[87,98,76,90]
}
ss = DataFrame(ddf,index=['语文','数学','英语','体育'])
ss
-----------------------------------------------------------
jobbobli
语文846587
数学988998
英语908676
体育918790
5.1 求期中期末平均值
ff= dd + ss
ff/2
-----------------------------
jobbobli
语文89.065.087.0
数学94.072.598.0
英语90.086.087.0
体育91.087.084.0
5.2 job的期末数学分数变成0
ss.iloc[1,0]=0
ss
-----------------------------------
jobbobli
语文847087
数学09498
英语909176
体育919290
5.3 bob所有的分数加五分
ss.iloc[:,1] +=5
ss
-----------------------------
jobbobli
语文847587
数学09998
英语909676
体育919790
好了,今天pandas的基础知识先学到这里啦,记得关注小编学习下一篇我们将会涉及pandas相对高级的用法,每天进步一点点。喜欢这篇文章记得点赞评论呀!!!
另外附上之前numpy的学习笔记的链接:

python数据分析学习笔记:numpy.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值