您的位置: 网站首页> Pandas教程> 当前文章
DataFrame的iloc位置索引,切片,布尔索引筛选单多行多列
老董-我爱我家房产SEO2021-10-25150围观,125赞
pandas中DataFrame选择数据可以用iloc选择器,注意iloc不是函数。iloc用于位置索引筛选,也可以用于布尔值筛选。
官方介绍如下:
Purely integer-location based indexing for selection by position。.iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.
我们按照这个df为例来进行学习。
df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c']) print(df)
pingtai pv uv a baidu 800 200 b sogou 500 100 c google 100 60
1、iloc位置索引选择单行,返回Series或者DataFrame
df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c']) print(df.iloc[0]) print('===') print(df.iloc[[0]])
pingtai baidu pv 800 uv 200 Name: a, dtype: object === pingtai pv uv a baidu 800 200
2、iloc位置索引选择多行及所有列,返回DataFrame
df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c']) print(df.iloc[[0,1]])
pingtai pv uv a baidu 800 200 b sogou 500 100
3、iloc位置索引选择多行多列,返回DataFrame
df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c']) print(df.iloc[[0,1],[1,2]])
pv uv a 800 200 b 500 100
4、iloc位置切片索引(末端不包含)选择多行及所有列,返回DataFrame
df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c']) print(df.iloc[[0,1],:])
pingtai pv uv a baidu 800 200 b sogou 500 100
5、iloc位置切片索引(末端不包含)选择多行多列,返回DataFrame
df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c']) print(df.iloc[0:2,0:2])
pingtai pv a baidu 800 b sogou 500
6、iloc布尔索引(末端不包含)选择多行多列,返回DataFrame
df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c']) print(df.iloc[[True,True,True],[False,True,True]])
pv uv a 800 200 b 500 100 c 100 60
很赞哦!
python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群
相关文章
文章评论
-
DataFrame的iloc位置索引,切片,布尔索引筛选单多行多列文章写得不错,值得赞赏
站点信息
- 网站程序:Laravel
- 客服微信:a772483200