Sorting, Reindexing, Renaming, Reshaping, Dropping
Sorting by values
# Sorting in Ascending order
print(df.sort_values('Price', ascending=True))
Output:
Fruits Quantity Price
c Banana 25 50
d Orange 10 70
a Mango 40 80
b Apple 20 100
# Sorting in Descending order
print(df.sort_values('Price', ascending=False))
Output:
Fruits Quantity Price
b Apple 20 100
a Mango 40 80
d Orange 10 70
c Banana 25 50
Sorting by Index
print(df.sort_index(ascending=False))
Output:
Fruits Quantity Price
d Orange 10 70
c Banana 25 50
b Apple 20 100
a Mango 40 80
Reindexing
# Reset the indexes to default
# inplace = True will make changes to the orginal dataframe
# drop =True will drop the initial indexes
df.reset_index(drop=True, inplace=True)
print(df)
Output:
Fruits Quantity Price
0 Mango 40 80
1 Apple 20 100
2 Banana 25 50
3 Orange 10 70
Renaming
df.rename(columns={'Fruits': 'FRUITS',
'Quantity': 'QUANTITY',
'Price': 'PRICE'},
inplace=True)
print(df)
Output:
FRUITS QUANTITY PRICE
0 Mango 40 80
1 Apple 20 100
2 Banana 25 50
3 Orange 10 70
Reshaping
A. Gather columns into rows.
# Gather columns into rows.
print(pd.melt(df))
Output:
variable value
0 FRUITS Mango
1 FRUITS Apple
2 FRUITS Banana
3 FRUITS Orange
4 QUANTITY 40
5 QUANTITY 20
6 QUANTITY 25
7 QUANTITY 10
8 PRICE 80
9 PRICE 100
10 PRICE 50
11 PRICE 70
B. Create a Pivot Table
# Pivot table
pivot = df.pivot(columns='FRUITS',
values=['PRICE', 'QUANTITY'])
print(pivot)
Output:
PRICE QUANTITY
FRUITS Apple Banana Mango Orange Apple Banana Mango Orange
0 NaN NaN 80.0 NaN NaN NaN 40.0 NaN
1 100.0 NaN NaN NaN 20.0 NaN NaN NaN
2 NaN 50.0 NaN NaN NaN 25.0 NaN NaN
3 NaN NaN NaN 70.0 NaN NaN NaN 10.0
Dropping
A. Drop column
# Drop the DISCOUNT Columns
df1 = df.drop(columns=['QUANTITY'], axis=1)
print(df1)
Output:
FRUITS PRICE
0 Mango 80
1 Apple 100
2 Banana 50
3 Orange 70
B. Drop rows
# Drop 2nd and 4th rows
df2 = df.drop([1, 3], axis=0)
print(df2)
Output:
FRUITS QUANTITY PRICE
0 Mango 40 80
2 Banana 25 50
Pandas Cheat Sheet for Data Science in Python
Pandas is a powerful and versatile library that allows you to work with data in Python. It offers a range of features and functions that make data analysis fast, easy, and efficient. Whether you are a data scientist, analyst, or engineer, Pandas can help you handle large datasets, perform complex operations, and visualize your results.
This Pandas Cheat Sheet is designed to help you master the basics of Pandas and boost your data skills. It covers the most common and useful commands and methods that you need to know when working with data in Python. You will learn how to create, manipulate, and explore data frames, how to apply various functions and calculations, how to deal with missing values and duplicates, how to merge and reshape data, and much more.
If you are new to Data Science using Python and Pandas, or if you want to refresh your memory, this cheat sheet is a handy reference that you can use anytime. It will save you time and effort by providing you with clear and concise examples of how to use Pandas effectively.