Python数据分析大杀器之Pandas基础2万字详解(学pandas基础,这一篇就够啦)

Python数据分析


  • 🌸个人主页:JoJo的数据分析历险记
  • 📝个人介绍:小编大四统计在读,目前保研到统计学top3高校继续攻读统计研究生
  • 💌如果文章对你有帮助,欢迎关注、点赞、收藏、订阅专栏

本专栏主要介绍python数据分析领域的应用
参考资料:
利用python数据分析

最近小伙伴问我有什么刷题网站推荐,在这里推荐一下牛客网,里面包含各种面经题库,全是免费的题库,可以全方面提升你的职业竞争力,提升编程实战技巧,赶快来和我一起刷题吧!牛客网链接|python篇

我们介绍了Numpy在数据处理方面的应用,本文介绍一下pandas在数据处理方面的应用,pandas可以是基于numpy构建的,但是可以让数据处理变得更便捷

导入相关库

import numpy as np
import pandas as pd

💮1.Series 对象

pandas主要有两个数据对象,一个是Series,类似于一个向量的形式,另一个是DataFrame数据框形式。我们先来看一下如何创建一个Series数据对象。

s = pd.Series([12,-4,7,9])
s
0    12
1    -4
2     7
3     9
dtype: int64

🏵️1.1 Series基本操作

#选择内部元素
s[2]
7
#为元素赋值
s[2]=5
s
s['a'] = 4
s
0    12
1    -4
2     5
3     9
a     4
dtype: int64
#用其它对象定义新的series对象
arr = np.array([1,2,3,4])
#此时s2只是原来的一个动态视图,会随数组的改变而改变,例如我们改变原来数组中的第二个元素值
s2 = pd.Series(arr)
s2
arr[1] = 9
s2


0    1
1    9
2    3
3    4
dtype: int32
#筛选元素
s[s>8]
0    12
3     9
dtype: int64
#Series对象的组成元素
serd = pd.Series([1,0,2,1,2,3], index=['white','white','blue','green','green','yellow'])
serd
white     1
white     0
blue      2
green     1
green     2
yellow    3
dtype: int64
#unique去重 返回一个数组
serd.unique()
array([1, 0, 2, 3], dtype=int64)
#value_counts 去重 返回出现次数
serd.value_counts()
2    2
1    2
3    1
0    1
dtype: int64
#isin 函数,返回布尔值
serd.isin([0,3])
white     False
white      True
blue      False
green     False
green     False
yellow     True
dtype: bool
serd[serd.isin([0,3])]
white     0
yellow    3
dtype: int64
#NaN
s2 = pd.Series([-5,3,np.NaN,14])
s2
0    -5.0
1     3.0
2     NaN
3    14.0
dtype: float64
# 用isnull 和 notnull 来进行判断
s2.isnull()
s2.notnull()
0     True
1     True
2    False
3     True
dtype: bool
s2

0    -5.0
1     3.0
2     NaN
3    14.0
dtype: float64
#用作字典
mydict = {'red':2000,'blue':1000,'yellow':500,'orange':1000}
myseries = pd.Series(mydict)
myseries
red       2000
blue      1000
yellow     500
orange    1000
dtype: int64

当出现缺失值时,会直接用NaN替代



colors = ['red','blue','yellow','orange','green']
myseries = pd.Series(mydict, index = colors)
myseries
red       2000.0
blue      1000.0
yellow     500.0
orange    1000.0
green        NaN
dtype: float64

进行运算时有NaN为NaN


mydict2 ={'red':400,'yellow':1000,"black":700}
myseries2 = pd.Series(mydict2)
myseries.fillna(0) + myseries2.fillna(0)
black        NaN
blue         NaN
green        NaN
orange       NaN
red       2400.0
yellow    1500.0
dtype: float64

🌹2.DataFrame对象

DataFrame对象是我们在进行数据分析时最常见的数据格式,相当于一个矩阵数据,由不同行不同列组成,通常每一列代表一个变量,每一行代表一个观察数据。我们先来看一下DataFrame的一些基础应用。

创建DataFrame对象

#DataFrame对象
data = {'color':['blue','green','yellow','red','white'],
        'object':['ball','pen','pencil','paper','mug'],
        'price':[1.2,1.0,0.6,0.9,1.7]}
frame = pd.DataFrame(data)
frame
colorobjectprice
0blueball1.2
1greenpen1.0
2yellowpencil0.6
3redpaper0.9
4whitemug1.7
frame2 = pd.DataFrame(data, columns=['object','price'])
frame2
objectprice
0ball1.2
1pen1.0
2pencil0.6
3paper0.9
4mug1.7
frame3 = pd.DataFrame(data,index=['one','two','three','four','five'])
frame3
colorobjectprice
oneblueball1.2
twogreenpen1.0
threeyellowpencil0.6
fourredpaper0.9
fivewhitemug1.7
#选取元素
#获得所有列的名称
frame.columns

Index(['color', 'object', 'price'], dtype='object')
#获得所有行的名称
frame.index
RangeIndex(start=0, stop=5, step=1)
#获得所有值
frame.values
array([['blue', 'ball', 1.2],
       ['green', 'pen', 1.0],
       ['yellow', 'pencil', 0.6],
       ['red', 'paper', 0.9],
       ['white', 'mug', 1.7]], dtype=object)
#获得某一列的值
frame['price']
0    1.2
1    1.0
2    0.6
3    0.9
4    1.7
Name: price, dtype: float64
#获得行的值 用ix属性和行的索引项
frame.iloc[2]

color     yellow
object    pencil
price        0.6
Name: 2, dtype: object
#指定多个索引值能选取多行
frame.iloc[[2,4]]
colorobjectprice
2yellowpencil0.6
4whitemug1.7
#可以用frame[0:1]或者frame[0:2]选择行  但切记frame[0]没有数
frame[0:4]

对DataFrame进行行选择时,使用索引frame[0:1]返回第一行数据,[1:2]返回第二行数据

colorobjectprice
0blueball1.2
1greenpen1.0
2yellowpencil0.6
3redpaper0.9
#如果要获取其中的一个元素,必须依次指定元素所在的列名称、行的索引值或标签
frame['object'][3]

'paper'
#赋值
frame['new']=12 #直接添加某一列
frame
colorobjectpricenew
0blueball1.212
1greenpen1.012
2yellowpencil0.612
3redpaper0.912
4whitemug1.712
frame['new']=[1,2,3,4,5]
frame
colorobjectpricenew
0blueball1.21
1greenpen1.02
2yellowpencil0.63
3redpaper0.94
4whitemug1.75
#修改单个元素的方法
frame['price'][2]=3.3
frame

colorobjectpricenew
0blueball1.21
1greenpen1.02
2yellowpencil3.33
3redpaper0.94
4whitemug1.75
# 删除一整列的所有数据,用del
frame['new'] = 12
frame
del frame['new']
frame
colorobjectprice
0blueball1.2
1greenpen1.0
2yellowpencil3.3
3redpaper0.9
4whitemug1.7
#筛选元素
frame3 = pd.DataFrame(np.arange(16).reshape((4,4)),index = ['red','white','blue','green'],
                      columns=['ball','pen','pencil','paper'])
frame3
frame3[frame3>12]
ballpenpencilpaper
redNaNNaNNaNNaN
whiteNaNNaNNaNNaN
blueNaNNaNNaNNaN
greenNaN13.014.015.0
#用嵌套字典生成DataFrame对象 当出现缺失值时用NaN替代
nestdict = {'red':{2012:22, 2013:33},'white':{2011: 13,2012:22,2013:16},'blue':{2011:17,2012:27,2013:48}}
nestdict
{'red': {2012: 22, 2013: 33},
 'white': {2011: 13, 2012: 22, 2013: 16},
 'blue': {2011: 17, 2012: 27, 2013: 48}}
frame2 = pd.DataFrame(nestdict)
frame2
redwhiteblue
2011NaN1317
201222.02227
201333.01648

进行转置

frame2.T
201120122013
redNaN22.033.0
white13.022.016.0
blue17.027.048.0
#index对象
ser = pd.Series([5,0,3,8,4], index=['red','blue','yellow','white','green'])
ser.index
Index(['red', 'blue', 'yellow', 'white', 'green'], dtype='object')
ser.idxmax()
'white'
ser.idxmin()
'blue'
#含重复标签的Index
serd = pd.Series(range(6), index=['white','white','blue','green','green','yellow'])
serd
white     0
white     1
blue      2
green     3
green     4
yellow    5
dtype: int64
#当一个标签对应多个元素时,返回一个Series对象 而不是单个元素
serd['white']
white    0
white    1
dtype: int64
#判断是否由重复值, is_unique
#索引对象的其他功能
ser = pd.Series([2,5,7,4],index = ['one','two','three','four'])
ser
one      2
two      5
three    7
four     4
dtype: int64
#reindex()函数可以更换series对象的索引,生成一个新的series对象
ser.reindex(['three','one','five','two'])
three    7.0
one      2.0
five     NaN
two      5.0
dtype: float64
ser3 = pd.Series([1,5,6,3],index=[0,3,5,6])
ser3
0    1
3    5
5    6
6    3
dtype: int64
#自动插补
#reindex()函数,method:ffill 表示插补的数为前面的值,bfill表示插补的数为后面的值
ser3.reindex(range(6),method='ffill')

0    1
1    1
2    1
3    5
4    5
5    6
dtype: int64
ser3.reindex(range(8),method='bfill')
0    1.0
1    5.0
2    5.0
3    5.0
4    6.0
5    6.0
6    3.0
7    NaN
dtype: float64
frame.reindex(range(5), method='ffill',columns=['colors','price','new','object'])
colorspricenewobject
0blue1.2blueball
1green1.0greenpen
2yellow3.3yellowpencil
3red0.9redpaper
4white1.7whitemug
ser = pd.Series(np.arange(4.),index=['red','blue','yellow','white'])
ser
red       0.0
blue      1.0
yellow    2.0
white     3.0
dtype: float64
ser.drop('yellow')
red      0.0
blue     1.0
white    3.0
dtype: float64
ser.drop(['blue','white'])
red       0.0
yellow    2.0
dtype: float64
frame = pd.DataFrame(np.arange(16).reshape((4,4)),
                    index=['red','blue','yellow','white'],
                    columns=['ball','pen','pencil','paper'])
frame
ballpenpencilpaper
red0123
blue4567
yellow891011
white12131415
#删除时默认是行 axis指定轴,1为列
frame.drop(['pen'],axis=1)
ballpencilpaper
red023
blue467
yellow81011
white121415

🥀3.pandas基本数据运算

🌺3.1 算术运算

  • 当有两个series或DataFrame对象时,如果一个标签,两个对象都有,则把他们的值相加
  • 当一个标签只有一个对象有时,则为NaN
s1 = pd.Series([3,2,5,1],index=['white','yellow','green','blue'])
s1
white     3
yellow    2
green     5
blue      1
dtype: int64
s2 = pd.Series([1,4,7,2,1],['white','yellow','black','blue','brown'])
s1 + s2
black     NaN
blue      3.0
brown     NaN
green     NaN
white     4.0
yellow    6.0
dtype: float64
# DateFrame对象也一样
frame1 = pd.DataFrame(np.arange(16).reshape((4,4)),
                     columns=['ball','pen','pencil','paper'],
                      index = ['red','blue','yellow','white'])
frame1
ballpenpencilpaper
red0123
blue4567
yellow891011
white12131415
frame2 = pd.DataFrame(np.arange(12).reshape((4,3)),
                     index = ['blue','yellow','green','white']
                     ,columns=['ball','pen','mug'])
frame2
ballpenmug
blue012
yellow345
green678
white91011
frame3 = frame1+frame2
frame3
ballmugpaperpenpencil
blue4.0NaNNaN6.0NaN
greenNaNNaNNaNNaNNaN
redNaNNaNNaNNaNNaN
white21.0NaNNaN23.0NaN
yellow11.0NaNNaN13.0NaN

🌻3.2 基本算术运算符

主要的算术运算符如下

  • add() frame1.add(frame2) = frame1+frame2
  • sub()
  • div()
  • mul()

下面通过一些案例来说明

frame = pd.DataFrame(np.arange(16).reshape((4,4)),
                     columns=['ball','pen','pencil','paper'],
                      index = ['red','blue','yellow','white'])
frame
ballpenpencilpaper
red0123
blue4567
yellow891011
white12131415
ser = pd.Series(np.arange(4),['ball','pen','pencil','paper'])
ser #与frame 的列名称保持一致,行不可以
ball      0
pen       1
pencil    2
paper     3
dtype: int32
frame-ser
ballpenpencilpaper
red0000
blue4444
yellow8888
white12121212

当索引项只存在于其中一个数据结构时,那么运算结果会为其产生一个新的索引项,但其值为NaN

具体案例如下,我们给ser增加一列mug

ser['mug'] = 9
ser
ball      0
pen       1
pencil    2
paper     3
mug       9
dtype: int64
frame - ser
ballmugpaperpenpencil
red0NaN000
blue4NaN444
yellow8NaN888
white12NaN121212

🌼3.3 函数映射

在dataframe和series数据对象中,可以使用函数对所有元素进行操作

frame
ballpenpencilpaper
red0123
blue4567
yellow891011
white12131415
# 求所有元素的平方根
np.sqrt(frame)

ballpenpencilpaper
red0.0000001.0000001.4142141.732051
blue2.0000002.2360682.4494902.645751
yellow2.8284273.0000003.1622783.316625
white3.4641023.6055513.7416573.872983
#定义函数
#法一:
f = lambda x:x.max()-x.min()#返回数组取值范围
#法二:
def f(x):
    return x.max()-x.min()

# apply函数可以调用定义的函数

frame.apply(f)
ball      12
pen       12
pencil    12
paper     12
dtype: int64
def f(x):
    return pd.Series([x.min(),x.max()],index = ['min','max'])
frame.apply(f,axis = 1)
# 默认axis=0
minmax
red03
blue47
yellow811
white1215

🌷4.统计函数

  • 数组大多数统计函数对DataFrame对象有用,故可以直接使用
frame.sum()
ball      24
pen       28
pencil    32
paper     36
dtype: int64
frame.mean()
ball      6.0
pen       7.0
pencil    8.0
paper     9.0
dtype: float64
# describe()函数可以计算多个统计量
frame.describe()
ballpenpencilpaper
count4.0000004.0000004.0000004.000000
mean6.0000007.0000008.0000009.000000
std5.1639785.1639785.1639785.163978
min0.0000001.0000002.0000003.000000
25%3.0000004.0000005.0000006.000000
50%6.0000007.0000008.0000009.000000
75%9.00000010.00000011.00000012.000000
max12.00000013.00000014.00000015.000000
```python ser.rank(method='first') ```
red       4.0
blue      1.0
yellow    2.0
white     5.0
green     3.0
dtype: float64

🌱4.1 相关性和协方差

🌲4.1.1 Series对象

  • 通常涉及两个数据对象
  • 函数分别corr()和cov()
seq = pd.Series([3,4,3,4,5,4,3,2],['2006','2007','2008','2009','2010','2011','2012','2013'])
seq
2006    3
2007    4
2008    3
2009    4
2010    5
2011    4
2012    3
2013    2
dtype: int64
seq2 = pd.Series([1,2,3,4,4,3,2,1],['2006','2007','2008','2009','2010','2011','2012','2013'])
seq2
2006    1
2007    2
2008    3
2009    4
2010    4
2011    3
2012    2
2013    1
dtype: int64
seq.corr(seq2)
0.7745966692414834
seq.cov(seq2)
0.8571428571428571

🌳4.1.2DataFrame对象

DataFrame对象计算相关性和协方差依然返回一个dataframe对象

frame2 = pd.DataFrame([[1,4,3,6],[4,5,6,1],[3,3,1,5],[4,1,6,4]],
                     index=['red','blue','yellow','white'],
                     columns=['ball','pen','pencil','paper'])
frame2
ballpenpencilpaper
red1436
blue4561
yellow3315
white4164
frame2.corr()
ballpenpencilpaper
ball1.000000-0.2760260.577350-0.763763
pen-0.2760261.000000-0.079682-0.361403
pencil0.577350-0.0796821.000000-0.692935
paper-0.763763-0.361403-0.6929351.000000
frame2.cov()
ballpenpencilpaper
ball2.000000-0.6666672.000000-2.333333
pen-0.6666672.916667-0.333333-1.333333
pencil2.000000-0.3333336.000000-3.666667
paper-2.333333-1.333333-3.6666674.666667

🌴4.1.3DataFrame和Series相关性

corrwith()可以计算DataFrame对象的列或行与Series对象或者其他DataFrame对象元素两两之间的相关性

ser
red       5
blue      0
yellow    3
white     8
green     4
dtype: int64
frame2.corrwith(ser)
ball     -0.140028
pen      -0.869657
pencil    0.080845
paper     0.595854
dtype: float64
frame2.corrwith(frame)
ball      0.730297
pen      -0.831522
pencil    0.210819
paper    -0.119523
dtype: float64

🌵4.2排序和秩

  • Series用sort_values()和rank(),默认是升序,使用ascending=False改变为升序,下同
  • DataFrame用sort_index(by=‘’)和rank()

对ser排序

ser.sort_values()
blue      0
yellow    3
green     4
red       5
white     8
dtype: int64

对ser求秩

ser.rank()
red       4.0
blue      1.0
yellow    2.0
white     5.0
green     3.0
dtype: float64

安装pen对frame进行排序

frame.sort_values(by='pen')
ballpenpencilpaper
red0123
blue4567
yellow891011
white12131415
ser = pd.Series([5,0,3,8,4],index=['red','blue','yellow','white','green'])
ser
red       5
blue      0
yellow    3
white     8
green     4
dtype: int64
ser.sort_index()
#按字母表顺序升序排列
blue      0
green     4
red       5
white     8
yellow    3
dtype: int64
ser.sort_index(ascending=False)
# 改为降序
yellow    3
white     8
red       5
green     4
blue      0
dtype: int64
ser.sort_values()
#根据值排序
blue      0
yellow    3
green     4
red       5
white     8
dtype: int64
frame
ballpenpencilpaper
red0123
blue4567
yellow891011
white12131415
frame.sort_index()
ballpenpencilpaper
blue4567
red0123
white12131415
yellow891011

axis代表轴,1表示纵轴,0表示横轴

frame.sort_index(axis=1)
ballpaperpenpencil
red0312
blue4756
yellow811910
white12151314

🌾5.Pandas缺失值处理

🌿5.1 创建NaN数据

  • 为数据赋NaN值 用np.nan
ser = pd.Series([0,1,2,np.NaN,9], index=['red','blue','yellow','white','green'])
ser
red       0.0
blue      1.0
yellow    2.0
white     NaN
green     9.0
dtype: float64
ser['white']
nan

☘️5.2 删除NaN

  • dropna()
  • ser[ser.notnull()]
  • DataFrame中去除时 为避免删除整行或整列,用how='all’来表示只删除所有元素均为NAN的行或列,如果使用how=‘any’,则只要这一列有缺失值就删除整列
frame3.dropna(how='all')
ballmugpaperpenpencil
blue4.0NaNNaN6.0NaN
white21.0NaNNaN23.0NaN
yellow11.0NaNNaN13.0NaN

🍀5.3 为NaN元素填充其他值

  • fillna()函数·
frame3.fillna(0)
ballmugpaperpenpencil
blue4.00.00.06.00.0
green0.00.00.00.00.0
red0.00.00.00.00.0
white21.00.00.023.00.0
yellow11.00.00.013.00.0

#若要将不同列的NaN换成不同元素,依次指定列名称及要替换成的元素即可

frame3.fillna({'ball':1,"pen":99})
ballmugpaperpenpencil
blue4.0NaNNaN6.0NaN
green1.0NaNNaN99.0NaN
red1.0NaNNaN99.0NaN
white21.0NaNNaN23.0NaN
yellow11.0NaNNaN13.0NaN

🍁6. 层级索引和分层统计

有时候我们需要对数据进行分层级的索引,具体看下面这个例子

mser = pd.Series(np.random.rand(8),index=[['white','white','white','blue','blue','red','red','red'],
                                         ['up','down','right','up','down','up','down','left']])
mser
white  up       0.323513
       down     0.080292
       right    0.503630
blue   up       0.201143
       down     0.173879
red    up       0.866267
       down     0.601906
       left     0.140885
dtype: float64
mser.index
MultiIndex(levels=[['blue', 'red', 'white'], ['down', 'left', 'right', 'up']],
           codes=[[2, 2, 2, 0, 0, 1, 1, 1], [3, 0, 2, 3, 0, 3, 0, 1]])
mser['white']
up       0.323513
down     0.080292
right    0.503630
dtype: float64
mser[:,'up']
white    0.323513
blue     0.201143
red      0.866267
dtype: float64
mser[:,'right']
white    0.50363
dtype: float64
mser['white','up']#可以得到某一特定元素的
0.32351250980575463

🍂6.1 unstack()函数和stack()函数

unstack把等级索引Series对象转换为一个简单的DataFrame对象,把第二列索引转换为相应的列,stack则相反,具体如下

mser.unstack() #将series转换为dataframe
mser.unstack().fillna(0)
downleftrightup
blue0.1738790.0000000.000000.201143
red0.6019060.1408850.000000.866267
white0.0802920.0000000.503630.323513
frame
ballpenpencilpaper
red0123
blue4567
yellow891011
white12131415
frame.stack()#将dataframe转换为series对象
red     ball       0
        pen        1
        pencil     2
        paper      3
blue    ball       4
        pen        5
        pencil     6
        paper      7
yellow  ball       8
        pen        9
        pencil    10
        paper     11
white   ball      12
        pen       13
        pencil    14
        paper     15
dtype: int32

dataframe对象的行与列也可以定义分层级索引

mframe = pd.DataFrame(np.arange(16).reshape((4,4)),
                     index = [['white','white','red','red'],['up','down','up','down']],
                     columns=[['pen','pen','paper','paper'],[1,2,1,2]])
mframe
penpaper
1212
whiteup0123
down4567
redup891011
down12131415

🍃6.2调整层级顺序

  • swaplevel()函数以要互换位置的两个层级的名称为参数,返回交换位置后的一个新对象,其中的个元素的顺序保持不变
mframe.columns.names = ['object','id']
mframe.index.names = ['colors','status']
mframe
objectpenpaper
id1212
colorsstatus
whiteup0123
down4567
redup891011
down12131415
mframe.swaplevel('colors','status')
objectpenpaper
id1212
statuscolors
upwhite0123
downwhite4567
upred891011
downred12131415

🌍6.3按层级统计数据

  • 直接把层级的名称赋给level选项
mframe.sum(level='colors')
objectpenpaper
id1212
colors
white46810
red20222426

若想对某一层级的列进行统计,则需要把axis的值设置为1

mframe.sum(level='id', axis=1)
id12
colorsstatus
whiteup24
down1012
redup1820
down2628

🌎7.数据导入

很多时候,我们要分析的数据来自电脑上保存的数据文件,本文介绍一下如何导入我们最常用的csv文件,后续我还会介绍如何导入json数据、以及连接SQL数据库等其他的方式来导入数据

import pandas as pd # 加载模块

df = pd.read_csv('student.csv')
df
Student 	ID 	name   age  gender
11        1111    Dw    3  Female
12        1112     Q   23    Male
13        1113     W   21  Female
idcolorbrand_xsidbrand_y
0ballwhiteOMGballABC
1pencilredABCpencilOMG
2pencilredABCpencilPOD
3penredABCpenPOD

🌏8.数据处理

  • 表连接
  • 拼接
  • 组合

🌐8.1 连接

使用merge()函数 类似sql中的多表连接

🎇8.1.1 内连接

import numpy as np
import pandas as pd
frame1 = pd.DataFrame({'id':['ball','pencil','pen','mug','ashtray'],
                      'price':[12.33,11.44,33.21,13.23,33.62]})
frame1
idprice
0ball12.33
1pencil11.44
2pen33.21
3mug13.23
4ashtray33.62
frame2 = pd.DataFrame({'id':['pencil','pencil','ball','pen'],
                      'color':['white','red','red','black']})
frame2
idcolor
0pencilwhite
1pencilred
2ballred
3penblack
pd.merge(frame1,frame2)
idpricecolor
0ball12.33red
1pencil11.44white
2pencil11.44red
3pen33.21black

上述返回的DataFrame对象由原来的两个DataFrame对象中ID相同的行组成 并且没有指定基于哪一列进行合并,实际应用中通常要指定连接条件, 用on来zhid

frame1 = pd.DataFrame({'id':['ball','pencil','pen','mug','ashtray'],
                      'color':['white','red','red','black','green'],
                      'brand':['OMG','ABC','ABC','POD','POD']})
frame1
idcolorbrand
0ballwhiteOMG
1pencilredABC
2penredABC
3mugblackPOD
4ashtraygreenPOD
frame2 = pd.DataFrame({'id':['pencil','pencil','ball','pen'],
                      'brand':['OMG','POD','ABC','POD']})
frame2
idbrand
0pencilOMG
1pencilPOD
2ballABC
3penPOD
pd.merge(frame1,frame2,on='id') # 以id进行合并
idcolorbrand_xbrand_y
0ballwhiteOMGABC
1pencilredABCOMG
2pencilredABCPOD
3penredABCPOD
pd.merge(frame1,frame2,on='brand') # 以brand进行合并
id_xcolorbrandid_y
0ballwhiteOMGpencil
1pencilredABCball
2penredABCball
3mugblackPODpencil
4mugblackPODpen
5ashtraygreenPODpencil
6ashtraygreenPODpen

当出现两个列的名称不一致的时候,使用left_on 和 right_on,例如,下面两个表,一个是id,一个是sid,我们相当于是用第一个表的id和第二个表的sid连接

frame2.columns = ['sid','brand']
frame2

sidbrand
0pencilOMG
1pencilPOD
2ballABC
3penPOD
pd.merge(frame1,frame2,left_on = 'id',right_on ='sid')
idcolorbrand_xsidbrand_y
0ballwhiteOMGballABC
1pencilredABCpencilOMG
2pencilredABCpencilPOD
3penredABCpenPOD

merge()函数默认的是内连接,上述结果中的键是由交叉操作出来的

🎉8.1.2 外连接

  • 连接类型用how选项指定
  • 左连接 共有的加上左边的
  • 右连接 共有的加上右边的
  • 外连接把所有的键整合到一起
frame2.columns=['id','brand']
pd.merge(frame1,frame2,how='outer')
idcolorbrand
0ballwhiteOMG
1pencilredABC
2penredABC
3mugblackPOD
4ashtraygreenPOD
5pencilNaNOMG
6pencilNaNPOD
7ballNaNABC
8penNaNPOD
pd.merge(frame1,frame2,how='left')
idcolorbrand
0ballwhiteOMG
1pencilredABC
2penredABC
3mugblackPOD
4ashtraygreenPOD
pd.merge(frame1,frame2,how='right')
idcolorbrand
0pencilNaNOMG
1pencilNaNPOD
2ballNaNABC
3penNaNPOD

要合并多个键,则把多个键给on选项

pd.merge(frame1,frame2,on=['id','brand'],how='outer')
idcolorbrand
0ballwhiteOMG
1pencilredABC
2penredABC
3mugblackPOD
4ashtraygreenPOD
5pencilNaNOMG
6pencilNaNPOD
7ballNaNABC
8penNaNPOD

🎊8.1.3 以索引作为键进行连接

#方法一
pd.merge(frame1,frame2,left_index=True,right_index=True)
id_xcolorbrand_xid_ybrand_y
0ballwhiteOMGpencilOMG
1pencilredABCpencilPOD
2penredABCballABC
3mugblackPODpenPOD
#方法二 用join函数 默认左连接
frame2.columns = ['id2','brand2']
frame1.join(frame2)
idcolorbrandid2brand2
0ballwhiteOMGpencilOMG
1pencilredABCpencilPOD
2penredABCballABC
3mugblackPODpenPOD
4ashtraygreenPODNaNNaN

🎄8.2拼接

  • numpy中的concatenation()函数可以用来进行拼接操作
  • pandas的concat()函数实现了按轴拼接的功能()
arr1 = np.arange(9).reshape(3,3)
arr1
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
arr2 = np.arange(6,15).reshape(3,3)
arr2
array([[ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])
np.concatenate([arr1,arr2],axis=1)#默认是axis=0
array([[ 0,  1,  2,  6,  7,  8],
       [ 3,  4,  5,  9, 10, 11],
       [ 6,  7,  8, 12, 13, 14]])
ser1 = pd.Series(np.random.rand(4), index = [1,2,3,4])
ser1
1    0.180191
2    0.061649
3    0.236378
4    0.105309
dtype: float64
ser2 = pd.Series(np.random.rand(4), index = [5,6,7,8])
ser2
5    0.935277
6    0.516146
7    0.210461
8    0.912048
dtype: float64
pd.concat([ser1,ser2])
1    0.180191
2    0.061649
3    0.236378
4    0.105309
5    0.935277
6    0.516146
7    0.210461
8    0.912048
dtype: float64
pd.concat([ser1,ser2],axis = 1)
01
10.180191NaN
20.061649NaN
30.236378NaN
40.105309NaN
5NaN0.935277
6NaN0.516146
7NaN0.210461
8NaN0.912048

默认是外连接

pd.concat([ser1,ser2],axis=1,join='inner')
01

如果想要创建等级索引,需要用keys选项来完成

pd.concat([ser1,ser2],keys=[1,2])
1  1    0.180191
   2    0.061649
   3    0.236378
   4    0.105309
2  5    0.935277
   6    0.516146
   7    0.210461
   8    0.912048
dtype: float64
pd.concat([ser1,ser2],axis=1,keys=[1,2])
12
10.180191NaN
20.061649NaN
30.236378NaN
40.105309NaN
5NaN0.935277
6NaN0.516146
7NaN0.210461
8NaN0.912048

🎋8.3组合

  • 当无法通过合并或者拼接方法组合数据用组合函数
  • combine_first()函数可以用来组合Series对象,同时对齐数据
ser1 = pd.Series(np.random.rand(5), index=[1,2,3,4,5])
ser1
1    0.708279
2    0.233048
3    0.030991
4    0.261291
5    0.379752
dtype: float64
ser2 = pd.Series(np.random.rand(4), index = [2,4,5,6])
ser2
2    0.017397
4    0.764295
5    0.407552
6    0.352605
dtype: float64
ser1.combine_first(ser2)
1    0.708279
2    0.233048
3    0.030991
4    0.261291
5    0.379752
6    0.352605
dtype: float64
pd.concat([ser1,ser2])
1    0.708279
2    0.233048
3    0.030991
4    0.261291
5    0.379752
2    0.017397
4    0.764295
5    0.407552
6    0.352605
dtype: float64

🏆文章推荐

Python数据可视化大杀器之Seaborn:学完可实现90%数据分析绘图

Python数据分析大杀器之Numpy详解

最近小伙伴问我有什么刷题网站推荐,在这里推荐一下牛客网,里面包含各种面经题库,全是免费的题库,可以全方面提升你的职业竞争力,提升编程实战技巧,赶快来和我一起刷题吧!牛客网链接|python篇

  • 34
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 33
    评论
### 回答1: Pandas是一个Python库,用于数据处理和分析。在数据分析中,预处理是非常重要的一步,因为它可以帮助我们清洗和转换数据,使其更适合进行分析。Pandas提供了一些强大的预处理功能,包括数据清洗、数据转换、数据重塑和数据合并等。在使用Pandas进行数据分析时,预处理是必不可少的一步。 ### 回答2: 在数据分析中,数据的预处理是一个必要的过程。它的主要目的是清洗数据,准备数据,以便后续分析。在Python中,pandas是一种广泛使用的数据处理库。pandas可以通过其高效的数据结构和操作方法来清洗和处理数据。在本文中,将介绍pandas预处理的一些常见技术。 一、读取数据 在pandas中,使用read_csv()函数读取CSV格式的数据文件,read_excel()函数读取Excel格式的数据文件。它们都有很多选项,可以根据具体文件的格式进行设置。 二、查看数据 在pandas中,使用以下函数来查看数据: 1. head() - 显示数据框的前几行; 2. tail() - 显示数据框的后几行; 3. columns - 显示数据框的列名; 4. shape - 显示数据框的行列数; 5. info() - 显示数据框的基本信息,包括每列的名称、非空值数量和数据类型。 三、数据清洗 在数据清洗中,有以下一些常见的技术: 1. 删除重复行:使用drop_duplicates()函数; 2. 替换空值:使用fillna()函数; 3. 删除空值:使用dropna()函数; 4. 更改数据类型:使用astype()函数。 四、数据准备 在数据准备中,有以下一些常见的技术: 1. 数据合并:使用merge()函数; 2. 数据筛选:使用loc()函数或者iloc()函数; 3. 数据分组:使用groupby()函数; 4. 数据排序:使用sort_values()函数。 五、数据分析数据分析中,有以下一些常见的技术: 1. 数据聚合:使用agg()函数; 2. 统计描述:使用describe()函数; 3. 数据可视化:使用matplotlib或者seaborn库。 综上所述,pandas预处理是数据分析中必不可少的一步。通过使用pandas提供的函数和方法,可以方便地清理和处理数据,使其更容易被分析。 ### 回答3: PandasPython中最强大的数据处理库之一,它提供了DataFrame和Series这两种数据结构,可以快速便捷地处理数据。在数据分析过程中,我们往往需要先对数据进行预处理,以便后续的分析。Pandas提供了一系列的方法和函数,可以帮助我们进行数据的预处理。 首先,在进行数据分析之前,我们需要了解自己所面对的数据类型和数据结构。Pandas中的DataFrame结构就是类似于表格的结构,每一行代表一个样本,每一列代表一个属性。Series则是一维的数组结构。通过pandas.read_csv(),我们可以读取CSV格式的数据,并转化为DataFrame结构。 接下来,我们要对数据进行一些基本的处理,例如数据清洗、数据去重、缺失值处理、异常值处理等。在数据清洗过程中,我们往往需要对数据进行一些特殊的处理,例如字符串的分割、合并、替换等操作,Pandas提供了一系列能够对文本进行操作的函数。在数据去重方面,我们可以使用drop_duplicates()函数,它可以去除DataFrame中的重复记录。在处理缺失值时,Pandas提供了一系列的函数,如fillna()函数、dropna()函数,可以方便地将NaN值变为其他有意义的值,或者删除缺失值的行或列。在异常值处理方面,我们可以使用isoutlier()函数来找到数据中的异常值,并进行处理。 在数据预处理完成后,我们可以对数据进行一些统计分析,例如计算小计、计算总计、分位数、极差、方差、标准差等统计指标。我们可以使用describe()函数来获得数据的统计描述,还可以使用groupby()函数来对数据分组,使用agg()函数对每组进行计算统计指标。此外,我们还可以对数据进行排序、丢弃、合并等操作。 总之,Pandas是一个非常强大的Python库,可以轻松处理数据预处理和数据处理方面的任务。Pandas作为数据分析和数据处理的基础库,使用熟练后可以在数据分析中发挥更大的作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 33
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JOJO数据科学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值