Pandas中的透视表

Pandas中的透视表

在这篇文章中,我们将看到Pandas中的Pivot表。让我们来讨论一些概念。

Pandas : Pandas是一个建立在NumPy库之上的开源库。它是一个Python软件包,提供各种数据结构和操作,用于处理数字数据和时间序列。它主要因导入和分析数据更容易而流行。Pandas速度很快,它对用户来说具有高性能和生产力。

透视表:透视表是一个统计表,它总结了一个更广泛的表格(如来自数据库、电子表格或商业智能程序)的数据。这种总结可能包括总和、平均数或其他统计数据,透视表以一种有意义的方式将这些数据分组。

步骤

  • Import Library (Pandas)
  • 导入/加载/创建数据。
  • 使用Pandas.pivot_table()方法有不同的变体。

在这里,我们将讨论一些数据框架上的透视表的变体,如下所示。

# import packages
import pandas as pd
  
# create data
df = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12, 
                          3: 13, 4: 67, 5: 89,
                          6: 90, 7: 56, 8: 34},
                     
                 'Name': {0: 'Ram', 1: 'Deep', 2: 'Yash',
                          3: 'Aman', 4: 'Arjun', 5: 'Aditya',
                          6: 'Akash', 7: 'Chalsea',
                          8: 'Divya'},
                     
                 'Marks': {0: 89, 1: 97, 2: 45,
                           3: 78, 4: 56, 5: 76,
                           6: 81, 7: 87, 8: 100},
                     
                 'Grade': {0: 'B', 1: 'A', 2: 'F',
                           3: 'C', 4: 'E', 5: 'C',
                           6: 'B', 7: 'B', 8: 'A'}})
  
# view data
display(df)

输出:

Pandas中的透视表

示例1:简单使用pivot_table()方法

# import packages
import pandas as pd
  
# create data
df = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12, 
                          3: 13, 4: 67, 5: 89,
                          6: 90, 7: 56, 8: 34},
                     
                 'Name': {0: 'Ram', 1: 'Deep', 2: 'Yash',
                          3: 'Aman', 4: 'Arjun', 5: 'Aditya',
                          6: 'Akash', 7: 'Chalsea',
                          8: 'Divya'},
                     
                 'Marks': {0: 89, 1: 97, 2: 45,
                           3: 78, 4: 56, 5: 76,
                           6: 81, 7: 87, 8: 100},
                     
                 'Grade': {0: 'B', 1: 'A', 2: 'F',
                           3: 'C', 4: 'E', 5: 'C',
                           6: 'B', 7: 'B', 8: 'A'}})
  
# simple use pivot_table() method
print(pd.pivot_table(df, index = ["ID"]))

输出 :

Pandas中的透视表

例子2:具有多列索引的透视表。

# import packages
import pandas as pd
  
# create data
df = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12, 
                          3: 13, 4: 67, 5: 89,
                          6: 90, 7: 56, 8: 34},
                     
                 'Name': {0: 'Ram', 1: 'Deep', 2: 'Yash',
                          3: 'Aman', 4: 'Arjun', 5: 'Aditya',
                          6: 'Akash', 7: 'Chalsea',
                          8: 'Divya'},
                     
                 'Marks': {0: 89, 1: 97, 2: 45,
                           3: 78, 4: 56, 5: 76,
                           6: 81, 7: 87, 8: 100},
                     
                 'Grade': {0: 'B', 1: 'A', 2: 'F',
                           3: 'C', 4: 'E', 5: 'C',
                           6: 'B', 7: 'B', 8: 'A'}})
# multiple columns with 
# pivot_table() method
display(pd.pivot_table(df, 
                     index = ["ID", "Name"]))

输出 :

Pandas中的透视表

例子3:带有汇总功能的透视表。

# import packages
import pandas as pd
import numpy as np
  
# create data
df = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12,
                          3: 13, 4: 67, 5: 89, 
                          6: 90, 7: 56, 8: 34},
                     
                   'Name': {0: 'Ram', 1: 'Deep',
                            2: 'Yash', 3: 'Aman',
                            4: 'Arjun', 5: 'Aditya',
                            6: 'Akash',7: 'Chalsea',
                            8: 'Divya'},
  
                   'Marks': {0: 89, 1: 97, 2: 45, 
                             3: 78, 4: 56, 5: 76,
                             6: 81, 7: 87, 8: 100},
  
                   'Grade': {0: 'B', 1: 'A', 2: 'F', 3: 'C',
                             4: 'E', 5: 'C', 6: 'B', 7: 'B',
                             8: 'A'}})
  
# Pivot Table with mean 
# aggregate function on marks
display(pd.pivot_table(df,
                     index = ["Grade"],
                     values = ["Marks"],
                     aggfunc = np.mean))

输出 :

Pandas中的透视表

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程