Tutorial: How to select Rows and Columns using Pandas (Python)

Tutorial: How to select Rows and Columns using Pandas (Python)
Photo by Claudia Chiavazza / Unsplash

Pandas is a powerful open-source library for data manipulation and analysis in Python. It offers easy-to-use data structures and analysis tools, making it valuable for data scientists, analysts, and developers working with structured data.

Install and start using Pandas Python Library for Data Engineering
Pandas is a powerful and popular open-source library for data manipulation and analysis in Python. It provides easy-to-use data structures and data analysis tools, making it a valuable tool for data scientists, analysts, and developers working with structured data. Features Features of Pandas:…

In the next short tutorial, you will find how to select rows and columns with Pandas.

To select specific rows and columns from a DataFrame in Pandas, you can use the loc and iloc methods.

Loc

loc is used for label-based indexing. It allows you to select rows and columns by their labels or index names.

Example:

# Selecting a single row by label
df.loc[3]

# Selecting multiple rows by label
df.loc[[1, 3, 5]]

# Selecting a single column by label
df.loc[:, 'column_name']

# Selecting multiple columns by label
df.loc[:, ['column1', 'column2']]

# Selecting specific rows and columns by label
df.loc[[1, 3, 5], ['column1', 'column2']]

Pandas iLoc

iloc is used for integer-based indexing. It allows you to select rows and columns by their integer positions.

Example:

# Selecting a single row by integer position
df.iloc[3]

# Selecting multiple rows by integer position
df.iloc[[1, 3, 5]]

# Selecting a single column by integer position
df.iloc[:, 0]

# Selecting multiple columns by integer position
df.iloc[:, [0, 2]]

# Selecting specific rows and columns by integer position
df.iloc[[1, 3, 5], [0, 2]]

These are some basic examples of how to select rows and columns using Pandas. Depending on your specific needs, you can apply additional conditions and operations to further filter and manipulate the data.








Read more




Open-source Apps

9,500+

Medical Apps

500+

Lists

450+

Dev. Resources

900+

/