Как создать сводную таблицу в Python с помощью Pandas?

Опубликовано: 27 Марта, 2022

Сводная таблица - это статистическая таблица, которая суммирует существенную таблицу, такую как большие наборы данных. Это часть обработки данных. Эта сводка в сводных таблицах может включать среднее значение, медианное значение, сумму или другие статистические термины. Сводные таблицы изначально связаны с MS Excel, но мы можем создать сводную таблицу на Python, используя Pandas, используя метод dataframe.pivot ().

Syntax : dataframe.pivot(self, index=None, columns=None, values=None, aggfunc)

Parameters –
index: Column for making new frame’s index.
columns: Column for new frame’s columns.
values: Column(s) for populating new frame’s values.
aggfunc: function, list of functions, dict, default numpy.mean

Example 1:
Let’s first create a dataframe that includes Sales of Fruits.

# importing pandas
import pandas as pd
  
# creating dataframe
df = pd.DataFrame({"Product" : ["Carrots", "Broccoli", "Banana", "Banana",
                                "Beans", "Orange", "Broccoli", "Banana"],
                   "Category" : ["Vegetable", "Vegetable", "Fruit", "Fruit",
                                 "Vegetable", "Fruit", "Vegetable", "Fruit"],
                   "Quantity" : [8, 5, 3, 4, 5, 9, 11, 8],
                   "Amount" : [270, 239, 617, 384, 626, 610, 62, 90]})
df

Output:

Get the total sales of each product

# creating pivot table of total sales
# product-wise aggfunc = "sum" will 
# allow you to obtain the sum of sales
# each product
pivot = df.pivot_table(index =["Product"],
                       values =["Amount"],
                       aggfunc ="sum")
print(pivot)

Output:

Get the total sales of each category

# creating pivot table of total 
# sales category-wise aggfunc = "sum"
# will allow you to obtain the sum of
# sales each product
pivot = df.pivot_table(index =["Category"], 
                       values =["Amount"], 
                       aggfunc ="sum")
print(pivot)

Output:

Get the total sales of by category and product both

# creating pivot table of sales
# by product and category both
# aggfunc = "sum" will allow you
# to obtain the sum of sales each
# product
pivot = df.pivot_table(index =["Product", "Category"], 
                       values =["Amount"], aggfunc ="sum")
print (pivot)

Output –

Get the Mean, Median, Minimum sale by category

# creating pivot table of Mean, Median,
# Minimum sale by category aggfunc = {"median",
# "mean", "min"} will get median, mean and 
# minimum of sales respectively
pivot = df.pivot_table(index =["Category"], values =["Amount"], 
                       aggfunc ={"median", "mean", "min"})
print (pivot)

Output –

Get the Mean, Median, Minimum sale by product

# creating pivot table of Mean, Median,
# Minimum sale by product aggfunc = {"median",
# "mean", "min"} will get median, mean and
# minimum of sales respectively
pivot = df.pivot_table(index =["Product"], values =["Amount"],
                       aggfunc ={"median", "mean", "min"})
print (pivot)

Выход:

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.