loc——通过行标签索引行数据
iloc——通过行号索引行数据
ix——通过行标签或者行号索引行数据(基于loc和iloc 的混合)
-
标签切片,如’a’:‘c’,与序列切片如0:2不同,后者不包含index=2的元素,前者包含结束标签’c’所在的行。
-
布尔类型数组作为标签,例如[True, False]等价于[‘a’,‘c’]
1.loc
import numpy as np
import pandas as pd
from pandas import *
from numpy import *
data=DataFrame(np.arange(16).reshape(4,4),index=list("ABCD"),columns=list("wxyz"))
print(data)
# w x y z
#A 0 1 2 3
#B 4 5 6 7
#C 8 9 10 11
#D 12 13 14 15
#loc
#行的选取
print(data.loc["A"])
print(type(data.loc["A"]))
#w 0
#x 1
#y 2
#z 3
#Name: A, dtype: int32
#<class 'pandas.core.series.Series'>
print(data.loc[["A"]])
print(type(data.loc[["A"]]))
# w x y z
#A 0 1 2 3
#<class 'pandas.core.frame.DataFrame'>
#综上,[]返回Series,[[]]返回DataFrame
print(data.loc["A","w"])
print(type(data.loc["A","w"]))
#0
#<class 'numpy.int32'>
print(data.loc[:,"w"])
print(type(data.loc[:,"w"]))
#A 0
#B 4
#C 8
#D 12
#Name: w, dtype: int32
#<class 'pandas.core.series.Series'>
print(data.loc["A":"C"])
print(type(data.loc["A":"C"]))
# w x y z
#A 0 1 2 3
#B 4 5 6 7
#C 8 9 10 11
#<class 'pandas.core.frame.DataFrame'>
print(data.loc["A":"C","w":"y"])
print(type(data.loc["A":"C","w":"y"]))
# w x y
#A 0 1 2
#B 4 5 6
#C 8 9 10
#<class 'pandas.core.frame.DataFrame'>
print(data.loc[["A","C"],["w","y"]])
print(type(data.loc[["A","C"],["w","y"]]))
# w y
#A 0 2
#C 8 10
#<class 'pandas.core.frame.DataFrame'>
print(data.loc[:,["w","y"]])
print(type(data.loc[:,["w","y"]]))
# w y
#A 0 2
#B 4 6
#C 8 10
#D 12 14
#<class 'pandas.core.frame.DataFrame'>
#列的选取
print(data["w"])#等同于print(data.loc[:,"w"])
#A 0
#B 4
#C 8
#D 12
#Name: w, dtype: int32
print(data.loc[:,"w"])
#A 0
#B 4
#C 8
#D 12
#Name: w, dtype: int32
print(data["w"].equals(data.loc[:,"w"]))#True
#根据特殊条件选取行列
print(data["w"]>5)
#A False
#B False
#C True
#D True
#Name: w, dtype: bool
print(data.loc[data["w"]>5])
# w x y z
#C 8 9 10 11
#D 12 13 14 15
print(data.loc[data["w"]>5,"w"])
print(type(data.loc[data["w"]>5,"w"]))
#C 8
#D 12
#Name: w, dtype: int32
#<class 'pandas.core.series.Series'>
print(data.loc[data["w"]>5,["w"]])
print(type(data.loc[data["w"]>5,["w"]]))
# w
#C 8
#D 12
#<class 'pandas.core.frame.DataFrame'>
print(data["w"]==0)
print(data.loc[lambda data:data["w"]==0])
print(type(data.loc[lambda data:data["w"]==0]))
#A True
#B False
#C False
#D False
#Name: w, dtype: bool
# w x y z
#A 0 1 2 3
#<class 'pandas.core.frame.DataFrame'>
#loc赋值
print(data)
# w x y z
#A 0 1 2 3
#B 4 5 6 7
#C 8 9 10 11
#D 12 13 14 15
data.loc[["A","C"],["w","x"]]=999
print(data)
# w x y z
#A 999 999 2 3
#B 4 5 6 7
#C 999 999 10 11
#D 12 13 14 15
2.iloc
data=DataFrame(np.arange(16).reshape(4,4),index=list("ABCD"),columns=list("wxyz"))
print(data)
# w x y z
#A 0 1 2 3
#B 4 5 6 7
#C 8 9 10 11
#D 12 13 14 15
print(data.iloc[0])
print(type(data.iloc[0]))
#w 0
#x 1
#y 2
#z 3
#Name: A, dtype: int32
#<class 'pandas.core.series.Series'>
#print(data.iloc["A"])报错
#print(data.loc[0])报错
print(data.loc[["A"]])
print(type(data.loc["A"]))
# w x y z
#A 0 1 2 3
#<class 'pandas.core.series.Series'>
3.iloc和loc差别
iloc是按照行数取值,而loc按着index名取值
data=DataFrame(np.arange(16).reshape(4,4),index=list("1234"),columns=list("wxyz"))
print(data)
# w x y z
#1 0 1 2 3
#2 4 5 6 7
#3 8 9 10 11
#4 12 13 14 15
print(data.iloc[0])
#w 0
#x 1
#y 2
#z 3
#Name: 1, dtype: int32
#print(data.loc[0])报错
参考:https://blog.csdn.net/boywaiter/article/details/86012620
今天的文章loc和iloc的用法和区别分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/4728.html