Net-informations.com

How to Select Rows from Pandas DataFrame

Pandas is built on top of the Python Numpy library and has two primarydata structures viz. one dimensional Series and two dimensional DataFrame . Pandas DataFrame can handle both homogeneous and heterogeneous data . You can perform basic operations on Pandas DataFrame rows like selecting, deleting, adding, and renaming.

Create a Pandas DataFrame with data

Selecting rows using []

You can use square brackets to access rows from Pandas DataFrame.

**Select rows starting from 2nd row position upto 4th row position of all columns.

Selected columns

You can specify the column names while retrieving data from DataFrame.

**Select rows starting from 2nd row position upto 4th row position of columns 'TotalMarks'and 'Grade' .

Selecting rows using loc[]

**Select rows starting from 2nd row position upto 4th row position of all columns.

Selected columns

While using loc, you can specify the column names while retrieving data from DataFrame.

**Select rows starting from 2nd row position upto 4th row position of columns 'TotalMarks'and 'Grade' .

Select rows based on condition using loc

**Select all rows from DataFrame where Grade is 'E'.

Using 'loc' and '!='

**Select all rows whose Grade does not equal 'E'.

Combine multiple conditions with & operator

**Select all rows from DataFrame where total marks greater than 50 and less than 79.

Selected columns using loc

**Retrieve Name, TotalMarks, Grade column where total marks greater than 50 and less than 79.
How to Select Rows from Pandas DataFrame

Using loc[] and isin()

**Select all rows where grade is 'A' or 'B'

Selected column using loc[] and isin()

**Select only Name, TotalMarks, Grade columns where grade is 'A' or 'B'

Using Dataframe.query()










net-informations.com (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About