Groupby and Multi-Index in Data Frame || Lesson 1.11 || Python for Data Science || Learning Monkey |

preview_player
Показать описание
Groupby and Multi-Index in Data Frame
In this class, We discuss Groupby and Multi-Index in Data Frame.
The reader should have prior knowledge of data frame methods. Click here.
Groupby and Multi-Index in Data Frame
Groupby Method
The groupby method is used to group the values in the data frame.
Take an example and try to understand the concept of groupby in the data frame.
We consider sample superstore data set. The discussion about the data set is done in our previous class.
In our data set, the sale is considered in four regions.
Central, south, east, and west regions.
We want to consider the total sale according to region. We have to group the data according to region.
To group the elements according to the region, we use the groupby method.
The groupby method will group the data according to the region and return the groupby object.
On the groupby object, we select the sales and sum all the sales according to region.
The final result is converted to a data frame.
The code to groupby region and sum sales is given below.
import pandas as pd

# Applying group by on Region and Region wise sum of sale
print(temp)

The result obtained after the above code consists of region values as index and sales sum as a data column.
Multi-Index using Groupby
If we apply the groupby method on two columns. We get multi-index.
We consider region and the category-wise sum of sales.
Example: Central region Furniture category total sale.
The categories available are furniture, office supplies, and Technology.
Groupby on multiple columns code is shown below.
# Groupby on Multiple columns we get multi-index
print(temp1)

In the output shown above, we have a multi-index. The first index is a region, and the second index is in the category.
How to access data using multi-index in a data frame?
Accessing data using multi-index
One way to access data using multi-index is by the xs method.
The code to access data is given below.
# xs method to access elements using multi index

In the first option, we have given the input argument central. The index related to the central region is displayed.
To consider the second index, we pass the argument category to the level parameter.
In our second example, we have given argument furniture and level as a category to access only furniture values.
Another way to access the data using multi-index by loc attribute
# accessing multi index using loc

In the above example, the argument central is taken to search in the first index.
, : is given to take all the columns in the data. We have only one column of sales.
A tuple of arguments is to be given to take multi-index.
The example of taking a tuple of arguments is shown below.

An example to represent column names is given below.
# we mention column name
An example of accessing multiple lines is given below.
Access multiple lines of data using slicing is given below.

Slicing can also be done using the slice function.
Examples using the slicing function are provided below.

Link for playlists:

Рекомендации по теме
Комментарии
Автор

Best tutorial out there in youtube for loc, multi-index and groupby. If you have come to this video, do watch it fully to gain a lot of knowledge. Thank you and keep more coming.

jananiadanurrammohan
Автор

What if we want to find max sales of a category in a particular region

rishabhjain-th