Friday, March 29, 2024
HomePythonChoose Row From a Dataframe in Python

Choose Row From a Dataframe in Python


Pandas dataframes are used to deal with tabular knowledge in Python. On this article, we are going to talk about the way to choose a row from a dataframe in Python. We may even talk about how we are able to use boolean operators to pick knowledge from a pandas dataframe.

Choose Row From a Dataframe Utilizing iloc Attribute

The iloc attribute comprises an _iLocIndexer object that works as an ordered assortment of the rows in a dataframe. The functioning of the iloc attribute is just like checklist indexing. You should use the iloc attribute to pick a row from the dataframe. For this, you’ll be able to merely use the place of the row contained in the sq. brackets with the iloc attribute to pick a row of a pandas dataframe as proven beneath.

myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
place=1
row=myDf.iloc[position]
print("The row at place {} is :{}".format(place,row))

Output:

The dataframe is:
   Class  Roll      Identify
0      1    11    Aditya
1      1    12     Chris
2      1    13       Sam
3      2     1      Joel
4      2    22       Tom
5      2    44  Samantha
6      3    33      Tina
7      3    34       Amy
The row at place 1 is :Class        1
Roll        12
Identify     Chris
Identify: 1, dtype: object

Right here, you’ll be able to observe that the iloc attribute provides the row on the specified place as output.

Choose Row From a Dataframe Utilizing loc Attribute in Python

The loc attribute of a dataframe works in the same method to the keys of a python dictionary.  The loc attribute comprises a _LocIndexer object that you need to use to pick rows from a pandas dataframe. You should use the index label contained in the sq. brackets with the loc attribute to entry the weather of a pandas sequence as proven beneath.

myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
index=2
row=myDf.loc[index]
print("The row at index {} is :{}".format(index,row))

Output:

The dataframe is:
   Class  Roll      Identify
0      1    11    Aditya
1      1    12     Chris
2      1    13       Sam
3      2     1      Joel
4      2    22       Tom
5      2    44  Samantha
6      3    33      Tina
7      3    34       Amy
The row at index 2 is :Class      1
Roll      13
Identify     Sam
Identify: 2, dtype: object

When you have outlined a customized index for a dataframe, you need to use the index worth of a row to pick the row from the pandas dataframe as proven beneath.

myDf=pd.read_csv("samplefile.csv",index_col=0)
print("The dataframe is:")
print(myDf)
index=1
row=myDf.loc[index]
print("The row at index {} is :{}".format(index,row))

Output:

The dataframe is:
       Roll      Identify
Class                
1        11    Aditya
1        12     Chris
1        13       Sam
2         1      Joel
2        22       Tom
2        44  Samantha
3        33      Tina
3        34       Amy
The row at index 1 is :       Roll    Identify
Class              
1        11  Aditya
1        12   Chris
1        13     Sam

When you have a multilevel index, you need to use the indices to pick rows from a dataframe as proven beneath.

myDf=pd.read_csv("samplefile.csv",index_col=[0,1])
print("The dataframe is:")
print(myDf)
index=(1,12)
row=myDf.loc[index]
print("The row at index {} is :{}".format(index,row))

Output:

The dataframe is:
                Identify
Class Roll          
1     11      Aditya
      12       Chris
      13         Sam
2     1         Joel
      22         Tom
      44    Samantha
3     33        Tina
      34         Amy
The row at index (1, 12) is :Identify    Chris
Identify: (1, 12), dtype: object

Choose Column Utilizing Column Identify in a Pandas Dataframe

To pick out a column from a dataframe, you need to use the column title with sq. brackets as proven beneath.

myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
column_name="Class"
column=myDf[column_name]
print("The {} column is :{}".format(column_name,column))

Output:

The dataframe is:
   Class  Roll      Identify
0      1    11    Aditya
1      1    12     Chris
2      1    13       Sam
3      2     1      Joel
4      2    22       Tom
5      2    44  Samantha
6      3    33      Tina
7      3    34       Amy
The Class column is :0    1
1    1
2    1
3    2
4    2
5    2
6    3
7    3
Identify: Class, dtype: int64

If you wish to choose multiple column from a dataframe, you’ll be able to cross an inventory of column names to the sq. brackets as proven beneath.

myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
column_names=["Class","Name"]
column=myDf[column_names]
print("The {} column is :{}".format(column_names,column))

Output:

The dataframe is:
   Class  Roll      Identify
0      1    11    Aditya
1      1    12     Chris
2      1    13       Sam
3      2     1      Joel
4      2    22       Tom
5      2    44  Samantha
6      3    33      Tina
7      3    34       Amy
The ['Class', 'Name'] column is :   Class      Identify
0      1    Aditya
1      1     Chris
2      1       Sam
3      2      Joel
4      2       Tom
5      2  Samantha
6      3      Tina
7      3       Amy

Boolean Masking in a Pandas Dataframe

Boolean masking is used to verify for a situation in a dataframe. After we apply a boolean operator on  a dataframe column, it returns a pandas sequence object containing True and False values primarily based on the situation as proven beneath.

myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
boolean_mask=myDf["Class"]>1
print("The boolean masks is:")
print(boolean_mask)

Output:

The dataframe is:
   Class  Roll      Identify
0      1    11    Aditya
1      1    12     Chris
2      1    13       Sam
3      2     1      Joel
4      2    22       Tom
5      2    44  Samantha
6      3    33      Tina
7      3    34       Amy
The boolean masks is:
0    False
1    False
2    False
3     True
4     True
5     True
6     True
7     True
Identify: Class, dtype: bool

You possibly can choose rows from a dataframe utilizing the boolean masks. For this, it’s essential cross the sequence containing the boolean masks to the sq. brackets operator as proven beneath.

myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
boolean_mask=myDf["Class"]>1
print("The boolean masks is:")
print(boolean_mask)
print("The rows during which class>1 is:")
rows=myDf[boolean_mask]
print(rows)

Output:

The dataframe is:
   Class  Roll      Identify
0      1    11    Aditya
1      1    12     Chris
2      1    13       Sam
3      2     1      Joel
4      2    22       Tom
5      2    44  Samantha
6      3    33      Tina
7      3    34       Amy
The boolean masks is:
0    False
1    False
2    False
3     True
4     True
5     True
6     True
7     True
Identify: Class, dtype: bool
The rows during which class>1 is:
   Class  Roll      Identify
3      2     1      Joel
4      2    22       Tom
5      2    44  Samantha
6      3    33      Tina
7      3    34       Amy

As a substitute of utilizing sq. brackets, you can too use the the place() methodology to pick rows from a dataframe utilizing boolean masking. The the place() methodology, when invoked on a dataframe, takes the boolean masks as its enter argument and returns a brand new dataframe containing the specified rows as proven beneath.

myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
boolean_mask=myDf["Class"]>1
print("The boolean masks is:")
print(boolean_mask)
print("The rows during which class>1 is:")
rows=myDf.the place(boolean_mask)
print(rows)

Output:

The dataframe is:
   Class  Roll      Identify
0      1    11    Aditya
1      1    12     Chris
2      1    13       Sam
3      2     1      Joel
4      2    22       Tom
5      2    44  Samantha
6      3    33      Tina
7      3    34       Amy
The boolean masks is:
0    False
1    False
2    False
3     True
4     True
5     True
6     True
7     True
Identify: Class, dtype: bool
The rows during which class>1 is:
   Class  Roll      Identify
0    NaN   NaN       NaN
1    NaN   NaN       NaN
2    NaN   NaN       NaN
3    2.0   1.0      Joel
4    2.0  22.0       Tom
5    2.0  44.0  Samantha
6    3.0  33.0      Tina
7    3.0  34.0       Amy

Within the above instance utilizing the the place() methodology, the rows the place the boolean masks has the worth False, NaN values are saved within the dataframe. You possibly can drop the rows containing NaN values as proven beneath.

myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
boolean_mask=myDf["Class"]>1
print("The boolean masks is:")
print(boolean_mask)
print("The rows during which class>1 is:")
rows=myDf.the place(boolean_mask).dropna()
print(rows)

Output:

The dataframe is:
   Class  Roll      Identify
0      1    11    Aditya
1      1    12     Chris
2      1    13       Sam
3      2     1      Joel
4      2    22       Tom
5      2    44  Samantha
6      3    33      Tina
7      3    34       Amy
The boolean masks is:
0    False
1    False
2    False
3     True
4     True
5     True
6     True
7     True
Identify: Class, dtype: bool
The rows during which class>1 is:
   Class  Roll      Identify
3    2.0   1.0      Joel
4    2.0  22.0       Tom
5    2.0  44.0  Samantha
6    3.0  33.0      Tina
7    3.0  34.0       Amy

You too can use logical operators to create boolean masks from two or extra circumstances as proven beneath.

myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
boolean_mask=(myDf["Class"]>1) & (myDf["Class"]<3)
print("The boolean masks is:")
print(boolean_mask)
print("The rows during which class>1 and <3 is:")
rows=myDf.the place(boolean_mask).dropna()
print(rows)

Output:

The dataframe is:
   Class  Roll      Identify
0      1    11    Aditya
1      1    12     Chris
2      1    13       Sam
3      2     1      Joel
4      2    22       Tom
5      2    44  Samantha
6      3    33      Tina
7      3    34       Amy
The boolean masks is:
0    False
1    False
2    False
3     True
4     True
5     True
6    False
7    False
Identify: Class, dtype: bool
The rows during which class>1 and <3 is:
   Class  Roll      Identify
3    2.0   1.0      Joel
4    2.0  22.0       Tom
5    2.0  44.0  Samantha

After creating the boolean masks, you need to use it to pick the rows the place the boolean masks comprises True as proven beneath.

Conclusion

On this article, we mentioned the way to choose a row from a dataframe. We additionally mentioned the way to choose a column from a dataframe and the way to choose a number of rows from a dataframe utilizing boolean masking.

To study extra about python programming, you’ll be able to learn this text on checklist comprehension. In case you are seeking to get into machine studying, you’ll be able to learn this text on regression in machine studying.

Beneficial Python Coaching

Course: Python 3 For Learners

Over 15 hours of video content material with guided instruction for freshmen. Learn to create actual world purposes and grasp the fundamentals.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments