Python Pandas Frequently Asked Questions

Learn Python Pandas Frequently Asked Questions and increase chances of selection during interview. These are all the concepts you need to learn about Pandas Dataframe to become an expert. So let’s get started.

Note: In this article we are using data from this sample file to explain the concepts. You may also refer to this for Python basics course.

How to read a CSV file using Pandas Dataframe?

import pandas as pd

df = pd.read_csv("full path of csv file including file extension")

How to read an Excel file using Pandas Dataframe?

import pandas as pd

df = pd.read_excel("full path of csv file including file extension")

How to read CSV data to Pandas Dataframe from web URL?

import pandas as pd
base_url = 'https://raw.githubusercontent.com/codeforamerica/ohana-api/master/data/sample-csv/addresses.csv'
df = pd.read_csv(base_url)
df

How to read CSV data to Pandas Dataframe where header is 2nd row?

import pandas as pd
df = pd.read_csv("full path of csv file including file extension", headers=1)
df

How to skip blank lines while reading CSV data to Pandas Dataframe?

import pandas as pd
df = pd.read_csv("full path of csv file including file extension", skip_blank_lines=True)
df

How to read only selected columns from CSV to Pandas Dataframe?

import pandas as pd

column_list = ['location_id', 'country', 'address_1']
df = pd.read_csv("full path of csv file including file extension", cols=columns_list)
df

How to change datatype while reading CSV to Pandas Dataframe?

import pandas as pd

# For changing all columns datatype to String
df = pd.read_csv("full path of csv file including file extension", dtype="str")
df

How to check total number of rows and columns in Pandas Dataframe?

import pandas as pd
df = pd.read_csv("full path of csv file including file extension")
df.shape

How to get column names from Pandas Dataframe?

import pandas as pd
df = pd.read_csv("full path of csv file including file extension")
col_names = df.columns
print(col_names)

How to check datatypes of columns in Pandas Dataframe?

import pandas as pd
df = pd.read_csv("full path of csv file including file extension")
df.dtypes

How to read pipe delimited CSV file in Pandas Dataframe?

import pandas as pd
df = pd.read_csv("full path of csv file including file extension", sep="|")
df

How to read first n records from Pandas Dataframe?

import pandas as pd
df = pd.read_csv("full path of csv file including file extension", sep="|")

# Use df.head() method to read first three records from pandas dataframe as shown below.

df.head(3)

How to read last n records from Pandas Dataframe?

import pandas as pd
df = pd.read_csv("full path of csv file including file extension", sep="|")

# Use df.head() method to read first three records from pandas dataframe as shown below.

df.tail(3)

Have you been liking Python Pandas Frequently Asked Questions? Do not forget to share your feedback in the comment box.

For pandas official documentation please refer to this link.

Scroll to Top