Python – Subset DataFrame by Column Name
Using Pandas library, we can perform multiple operations on a DataFrame. We can even create and access the subset of a DataFrame in multiple formats. The task here is to create a subset DataFrame by column name. We can choose different methods to perform this task. Here are possible methods mentioned below –
Before performing any action, we need to write few lines of code to import necessary libraries and create a DataFrame.
Creating the DataFrame
Method 1: Using Python iloc() function
This function allows us to create a subset by choosing specific values from columns based on indexes.
Example: Create a subset with Name, Gender and Branch column
Method 2: Using Indexing Operator
We can use the indexing operator i.e. square brackets to create a subset dataframe
Example: Create a subset with Name, pre_1, and pre_2 column
Method 3: Using filter() method with like keyword
We can use this method particularly when we have to create a subset dataframe with columns having similarly patterned names.
Example: Create a subset with pre_1 and pre_2 column
Please Login to comment...