Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconPandas Cheatsheet: Top Commands You Should Know [2024]

Pandas Cheatsheet: Top Commands You Should Know [2024]

Last updated:
5th Oct, 2022
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
Pandas Cheatsheet: Top Commands You Should Know [2024]

Data analysis has become a new genre of study, and all thanks to Python. If you are an enthusiast data analyst who works on Python almost absolutely use the Pandas library, then this article is for you. This Pandas cheatsheet will go through all the essential methods that come in handy while analyzing data.

You might have encountered situations where it is hard to remember the specific syntax for doing something in Pandas. These Pandas cheat sheet commands will help you easily remember and reference the most common Pandas operations. If you are a beginner in python and data science, upGrad’s data science courses can definitely help you dive deeper into the world of data and analytics.

Using the Pandas Cheatsheet

Before using this Pandas cheat sheet, you should thoroughly learn Pandas Tutorial and then refer to this cheat sheet for remembering and clearance. Pandas cheat sheet will help you quickly look for methods you have already learned, and it can come in handy even if you are going for an exam or interview. We have collected and grouped all the commands used frequently in the Pandas by a data analyst for easy detection. In this Pandas cheat sheet, we will use the following shorthand for representing different objects.

  •  df: For representing any Pandas DataFrame object
  • ser: For representing any Pandas Series object

You have to use these following relevant libraries for implementing the methods mentioned below in this article.

  • import pandas as pd
  • import numpy as np 

Must Read: Pandas Interview Questions

1. Import data from different files

  • To read all data from a CSV file: pd.read_csv(file_name)
  • To read all data from a delimited text file (like TSV): pd.read_table(file_name)
  • To read from an Excel sheet: pd.read_excel(file_name)
  • To read data from a SQL database: pd.read_sql(query, connectionObject)
  • Fetching the data from a JSON formatted string or URL: pd.read_json(jsonString)
  • To take the contents of your clipboard: pd.read_clipboard()

2. Export DataFrames in different file formats

  • To write a DataFrame to a CSV file: df.to_csv(file_name)
  • To write a DataFrame to an Excel file: df.to_excel(file_name)
  • To write a DataFrame to a SQL table: df.to_sql(tableName, connectionObject)
  • To write a DataFrame to a file in JSON format: df.to_json(file_name)

3. Inspect a particular section of your DataFrame or Series

  • To fetch all the information related to index, datatype, and memory: df.info()
  • To extract the starting ‘n’ rows of your DataFrame: df.head(n)
  • To extract the ending ‘n’ rows of your DataFrame: df.tail(n)
  • To extract the number of rows and columns available in your DataFrame: df.shape
  • To summarize the statistics for numerical columns: df.describe()
  • To view unique values along with their counts: ser.value_counts(dropna=False)

4. Selecting a specific subset of your data

  • Extract the first row: df.iloc[0,:]
  • To extract the first element of your DataFrame’s first column: df.iloc[0,0]
  • To return columns having label ‘col’ as Series: df[col]
  • To return columns having a new DataFrame: df[[col1,col2]]
  • To select data by position: ser.iloc[0]
  • To select data by index: ser.loc[‘index_one’]

upGrad’s Exclusive Data Science Webinar for you –

ODE Thought Leadership Presentation

 

5. Data Cleaning Commands

  • To rename columns in masses: df.rename(columns = lambda x: x + 1)
  • To rename columns selectively: df.rename(columns = {‘oldName’: ‘newName’})
  • To rename the index in masses: df.rename(index = lambda x: x + 1)
  • To rename columns in sequence: df.columns = [‘x’, ‘y’, ‘z’]
  • To check if null values exists, returns a boolean arrray accordingly: pd.isnull()
  • The reverse of pd.isnull(): pd.notnull()
  • Drops all rows containing null values: df.dropna()
  • Drops all columns containing null values: df.dropna(axis=1)
  • To replace each null value with ‘n’: df.fillna(n)
  • To convert all the datatypes of the series into float: ser.astype(float)
  • To replace all numbered 1 with ‘one’ and 3 with ‘three’: ser.replace([1,2], [‘one’,’two’]) 

Also Read: Pandas Dataframe Astype

Explore our Popular Data Science Courses

6. Groupby, Sort, and Filter Data

  • To return a groupby object for column values: df.groupby(colm)
  • To return groupby object for multiple column values: df.groupby([colm1, colm2])
  • To sort values in ascending order (by column): df.sort_values(colm1)
  • To sort values in descending order (by column): df.sort_values(colm2, ascending=False)
  • Extract rows where the column value is greater than 0.6: df[df[colm] > 0.6]

Read our popular Data Science Articles

7. Others

  • Add the rows of the first DataFrame to the end of the second DataFrame: df1.append(df2)
  • Add the columns of the first DataFrame to the end of the second DataFrame: pd.concat([df1,df2],axis=1)
  • To return the mean of all columns: df.mean()
  • To return the number of non-null values: df.count()

Top Data Science Skills to Learn

Conclusion

These Pandas cheat sheets will be useful only for rapid recall. It is always a good approach to practice the commands before directly jumping into the Pandas cheat sheet.

If you are curious to learn about Pandas, check out IIIT-B & upGrad’s Executive PG Programme in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

Frequently Asked Questions (FAQs)

1What are the salient features of Pandas libraries?

The following are the features that make Pandas one of the most popular Python libraries: Pandas provides us with various data frames that not only allow efficient data representation but also enable us to manipulate it. It provides efficient alignment and indexing features that provide intelligent ways of labelling and organizing the data. Some features of Pandas make the code clean and increase its readability, thus making it more efficient. It can also read multiple file formats. JSON, CSV, HDF5, and Excel are some of the file formats supported by Pandas. The merging of multiple datasets has been a real challenge for many programmers. Pandas overcome this too and merge multiple data sets very efficiently. Pandas library also provides access to other important Python libraries like Matplotlib and NumPy which makes it a highly efficient library.

2What are the other libraries and tools that complement Pandas library?

Pandas not only works as a central library for creating data frames, but it also works with other libraries and tools of Python to be more efficient. Pandas is built on the NumPy Python package which indicates that most of Pandas library structure is replicated from the NumPy package. Statistical analysis on the data in Pandas library is operated by SciPy, plotting functions on Matplotlib, and machine learning algorithms in Scikit-learn. Jupyter Notebook is a web-based interactive environment that works as an IDE and offers a good environment for Pandas.

3State the basic operations of the data frame

Selecting an index or a column before starting any operation like addition or deletion is important. Once you learn how to access values and select columns from a Data Frame, you can learn to add index, row, or column in a Pandas Dataframe. If the index in the data frame does not come out to be as you desired, you can reset it. For resetting the index, you can use the “reset_index()” function.

Explore Free Courses

Suggested Blogs

Top 13 Highest Paying Data Science Jobs in India [A Complete Report]
905097
In this article, you will learn about Top 13 Highest Paying Data Science Jobs in India. Take a glimpse below. Data Analyst Data Scientist Machine
Read More

by Rohit Sharma

12 Apr 2024

Most Common PySpark Interview Questions & Answers [For Freshers & Experienced]
20861
Attending a PySpark interview and wondering what are all the questions and discussions you will go through? Before attending a PySpark interview, it’s
Read More

by Rohit Sharma

05 Mar 2024

Data Science for Beginners: A Comprehensive Guide
5064
Data science is an important part of many industries today. Having worked as a data scientist for several years, I have witnessed the massive amounts
Read More

by Harish K

28 Feb 2024

6 Best Data Science Institutes in 2024 (Detailed Guide)
5151
Data science training is one of the most hyped skills in today’s world. Based on my experience as a data scientist, it’s evident that we are in
Read More

by Harish K

28 Feb 2024

Data Science Course Fees: The Roadmap to Your Analytics Career
5075
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.
Read More

by Harish K

28 Feb 2024

Inheritance in Python | Python Inheritance [With Example]
17600
Python is one of the most popular programming languages. Despite a transition full of ups and downs from the Python 2 version to Python 3, the Object-
Read More

by Rohan Vats

27 Feb 2024

Data Mining Architecture: Components, Types & Techniques
10775
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a
Read More

by Rohit Sharma

27 Feb 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
80611
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

19 Feb 2024

Sorting in Data Structure: Categories & Types [With Examples]
139002
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e
Read More

by Rohit Sharma

19 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon