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.

01 – 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")

02 – 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")

03 – 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

04 – 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

05 – 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

06 – 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

07 – 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

08 – 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

09 – 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)

10 – 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

11 – 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

12 – 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)

13 – 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.

Leave a Comment

%d bloggers like this: