In this post, we’ll look at how to import an excel file into a data frame and find a particular column.

Let us take an example of demo.xlsx excel spreadsheet which is shown below:

How to Import Excel File and Find a Specific Column using Pandas?

Approach:

  • Import pandas module as pd using the import keyword.
  • Read the excel file using the read_excel() function and store it as a DataFrame(creating a dataframe).
  • Check the particular column and print it using the head() function. The head() function gives only the first 5 rows of a file.
  • The Exit of the Program.

Step1: Read an Excel File and get the first 5 rows of it.

Below is the implementation:

# Import pandas module as pd using the import keyword
import pandas as pd
# Read the excel file using the read_excel() function and 
# store it as a DataFrame
data_frme = pd.read_excel('demo.xlsx')  
# Get the first 5 rows of the excel file using the head() function
data_frme.head()

Output:

Step2: Check the particular column and display the first 5 values in that column using the head() function.

Below is the implementation:

# Import pandas module as pd using the import keyword
import pandas as pd
# Read the excel file using the read_excel() function and 
# store it as a DataFrame
data_frme= df = pd.read_excel('demo.xlsx')  
# Check the particular column and display the first 5 values in that column using the 
# head() function
# Here check if the Gender is female and display the first 5 values in that column
data_frme[data_frme["Gender"] == 'Female'].head()

Output:

Step3: Do the same for other columns

1)Here check if the Age is greater than 30 and display the first 5 values in that column.

# Import pandas module as pd using the import keyword
import pandas as pd
# Read the excel file using the read_excel() function and 
# store it as a DataFrame
data_frme= df = pd.read_excel('demo.xlsx')  
# Check the particular column and display the first 5 values in that column using the 
# head() function
# Here check if the Age is greater than 30 and display the first 5 values in that column
data_frme[data_frme["Age"]>30].head()

Output:

2)Here check if the Country is ‘United States and display the first 5 values in that column

# Import pandas module as pd using the import keyword
import pandas as pd
# Read the excel file using the read_excel() function and 
# store it as a DataFrame
data_frme= df = pd.read_excel('demo.xlsx')  
# Check the particular column and display the first 5 values in that column using the 
# head() function
# Here check if the Country is 'United States and display the first 5 values in that column
data_frme[data_frme["Country"]=='United States'].head()

Output:

Leave a Reply

Your email address will not be published.