Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Loc and iloc in pandas

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use Loc and iloc in pandas. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.

Loc and iloc

Loc and iloc are often used to select rows and columns, and their functions are similar, but their usage is different.

Usage:

years = ['2010','2014','2017'] df[df.year.isin(years)]

loc: Select rows and columns by label (column and index)

iloc: Select rows and columns by index position

Select the data in rows 1~3 and columns 1~2 of df, and use iloc:

df.iloc[:3,:2]

Use loc:

df.loc[:2,['group','year']]1

Tip: When loc is used, the index refers to the index value, including the upper boundary. The iloc index refers to the position of the row, excluding the upper boundary.

Select rows 1, 3 and 5, year and value_1 columns:

df.loc[[1,3,5],['year','value_1']]

About "how to use Loc and iloc in pandas" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report