pandas中loc和iloc用法

这篇博客介绍了pandas中loc和iloc的区别。loc通过行索引或列名选取数据,iloc则依据行号和列号。在创建DataFrame后,文章详细展示了如何取行和列,以及在取多行时遇到的问题。loc可用于根据特定条件提取行,而iloc精确到行列位置。此外,文章还探讨了等值筛选和非等值筛选的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

loc函数:通过行索引 “Index” 或者"columns"中的具体值来取行、列数据
iloc函数:通过行号或者列号来取行、列数据(如取第二行的数据、取第0列的数据)
首先我们创建一个Data Frame

import numpy as np
import pandas as pd
#创建一个Dataframe
data=pd.DataFrame(np.arange(16).reshape(4,4),index=list('abcd'),columns=list('ABCD'))
data
out:
	A	B	C	D
a	0	1	2	3
b	4	5	6	7
c	8	9	10	11
d	12	13	14	15

1.取行
方法一

#用loc
data.loc['a'] #取行index为‘a’的数据
#用iloc
data.iloc[0]#取第0行数据
#out:
A    0
B    1
C    2
D    3
Name: a, dtype: int64

想得到的结果为DataFrame格式?
方法二

data.loc[['a']] 
data.iloc[[0]]
#out:
	A	B	C	D
a	0	1	2	3

想取好几行?

data.loc[['a','d']] #取行index为‘a'和‘d'的数据
#out:
	A	B	C	D
a	0	1	2	3
d	12	13	14	15

data.iloc[[0,1]]	#取第0、1行的数据
#out:
	A	B	C	D
a	0	1	2	3
b	4	5	6	7

用方法一试试取好几行?

data.iloc[0,1]
#out:
1

怎么和想的中不一样?

data.loc['a','d']
#out:
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/Library/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py in _validate_key(self, key, axis)
   1789                 if not ax.contains(key):
-> 1790                     error()
   1791             except TypeError as e:

/Library/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py in error()
   1784                                .format(key=key,
-> 1785                                        axis=self.obj._get_axis_name(axis)))
   1786 

KeyError: 'the label [d] is not in the [index]'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-41-a9c720cf0498> in <module>()
----> 1 data.loc['a','d']

呀!怎么报错了?提示说index里没有标签d,咋回事,不急不急,我们先看列操作,这个问题稍后解决
2.取列

data.iloc[:,[0,3]] #取第0列和第三列
data.loc[:,['A','D']] #取A列和D列
#out:

	A	D
a	0	3
b	4	7
c	8	11
d	12	15

如果我们想取指定的行和列呢?
3.取指定的行、列

data.loc[['a','d'],['A','B']] #提取index为'a','d',列名为'A','B'中的数据
data.iloc[[0,3],[0,1]] #取第0行、3行,第0列、1列的数据
#out:
	A	B
a	0	1
d	12	13

现在我们回过头看1中遇到的问题:
data.iloc[0,1]表示的是取第0行第1列的数据,所以取出来的值只有1个
data.loc[‘a’,‘d’]表示的是取行index为‘a’,列名为‘d’的数据,并不是取行index为‘a’、‘d’的数据,所以报错来
4.利用loc函数,根据某个数据来提取数据所在的行

data.loc[data['B']==13] #取‘B’列值为13所在的行
#out:
	A	B	C	D
d	12	13	14	15
data.loc[(data['A']==12)&(data['B']==13)] #同时满足“A”列的值为12,“B”列的值为13
#out:
	A	B	C	D
d	12	13	14	15
data.loc[(data['A']==0)|(data['B']==13)]#同时满足“A”列的值为0或者“B”列的值为13
#out:
	A	B	C	D
a	0	1	2	3
d	12	13	14	15

同时,以下几种写法也可提取数据所在的行

data[data['B']==13]#dataframe用法
data[data['B'].isin([13])]#isin函数
data[(data['A']==12)&(data['B']==13)] #dataframe用法
data[(data['A'].isin([12]))&(data['B'].isin([13]))] #isin函数
#out:
	A	B	C	D
d	12	13	14	15
  1. 选取不等于某些值的行记录
data.loc[data['B']!=13]
data.loc[~data['B'].isin([13])]
#out:
	A	B	C	D
a	0	1	2	3
b	4	5	6	7
c	8	9	10	11

== 和!= 、 isin 和 ~ isin两组的区别在于第一组筛选的是等于或者不等于某个值的行,第二组筛选的是等于或者不等于某些值的行

data.loc[data['B'].isin([1,5])]
#out:
	A	B	C	D
a	0	1	2	3
b	4	5	6	7
data.loc[~data['B'].isin([1,5])]
#out:
	A	B	C	D
c	8	9	10	11
d	12	13	14	15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值