數據透視表和數據交叉表_數據透視表的數據提取

數據透視表和數據交叉表

Consider the data of healthcare drugs as provided in the excel sheet. The concept of pivot tables in python allows you to extract the significance from a large detailed dataset. A pivot table helps in tracking only the required information from the data frames. It summarizes the data. The panda’s pivot table is a good alternative instead of using other tools for analysing the data. The following are the features for using panda’s pivot table.

考慮excel表中提供的保健藥物數據。 python中數據透視表的概念使您可以從大型詳細數據集中提取重要性。 數據透視表有助于僅跟蹤數據幀中所需的信息。 它匯總了數據。 熊貓的數據透視表是一個很好的選擇,而不是使用其他工具來分析數據。 以下是使用熊貓數據透視表的功能。

  • It is quicker.

    它更快。
  • It is self-documenting

    它是自我記錄
  • It is easy to use for generating a report or email

    易于生成報告或電子郵件
  • It is more flexible in defining the aggregation functions.

    定義聚合函數時更加靈活。

Consider this below dataset for execution. Download the dataset which is available here. In this article, you will learn python pivots with many examples.

在下面的數據集中考慮執行。 下載可在此處獲得的數據集。 在本文中,您將通過許多示例學習python數據透視。

The pivot table must have a data frame and an index. Run the below code which has used “Patient Name” as an index.

數據透視表必須具有數據框和索引。 運行以下使用“患者姓名”作為索引的代碼。

Example 1:

范例1:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\drugs.xlsx”)data = pd.pivot_table(df,index=[“Patient Name”])print(data)
Image for post

The pd.pivot_table(df,index=[“Patient Name”])will track the data with “Patient Name” as index. We can also track the data with multiple indexes by providing multiple arguments to the index variable. Run the below Example 2 to see the result.

pd.pivot_table(df,index = [“患者名稱”])將以“患者名稱”作為索引來跟蹤數據。 我們還可以通過為index變量提供多個參數來跟蹤具有多個索引的數據。 運行下面的示例2以查看結果。

Example 2:

范例2:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\drugs.xlsx”)data = pd.pivot_table(df,index=[“Patient Name”,”Jr Doctor”,”Sr Doctor”])pd.set_option(‘display.max_columns’,None)print(data)
Image for post
Image for post

In the above program I have used pd.set_option(‘display.max_columns’,None)for displaying all the columns in a data frame so as to make it visible in a terminal output window during the program execution. The pd.pivot_table(df,index=[“Patient Name”,”Jr Doctor”,”Sr Doctor”])data will now track the data with the corresponding index names. For suppose if we don’t want Serial and Units columns as it is not useful than we can explicitly remove these columns by providing the values field. Run the below code of Example 3 to see the result.

在上面的程序中,我使用了pd.set_option('display.max_columns',None)來顯示數據框中的所有列,以便在程序執行期間使其在終端輸出窗口中可見。 現在,pd.pivot_table(df,index = [“患者姓名”,“小醫生”,“高級醫生”])數據將跟蹤具有相應索引名的數據。 假設如果我們不想使用Serial和Units列,因為它沒有用,那么我們可以通過提供values字段顯式刪除這些列。 運行下面的示例3的代碼以查看結果。

Example 3:

范例3:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\drugs.xlsx”)data = pd.pivot_table(df,index=[“Sr Doctor”,”Jr Doctor”],values=[“Units”])pd.set_option(‘display.max_columns’,None)print(data)
Image for post

We can use aggfunc and np.sum to get the count or a sum for the cost column automatically by using it as an argument in pivot_table()method.

我們可以使用aggfunc和np.sum來自動獲取成本列的計數或總和,方法是將其用作pivot_table()方法中的參數。

Example 4:

范例4:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\drugs.xlsx”)data = pd.pivot_table(df,index=[“Sr Doctor”,”Jr Doctor”],values=[“Cost”],aggfunc=np.sum)pd.set_option(‘display.max_columns’,None)print(data)
Image for post

The aggfunc argument can take list of functions. We can compute the mean using numpy and len to get the count.

aggfunc參數可以接受功能列表。 我們可以使用numpy和len計算平均值以獲得計數。

Example 5:

范例5:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\drugs.xlsx”)data = pd.pivot_table(df,index=[“Sr Doctor”,”Jr Doctor”],values=[“Cost”],aggfunc=[np.sum,len])pd.set_option(‘display.max_columns’,None)print(data)
Image for post

The columns are optional. They provide an additional way to segment the actual values you care about. The aggregation functions are applied to the values you list. Look at Example 6 for understanding.

列是可選的。 它們提供了另外一種細分實際值的方法。 聚合函數將應用于您列出的值。 請參閱示例6進行理解。

Example 6:

范例6:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\drugs.xlsx”)data = pd.pivot_table(df,index=[“Sr Doctor”,”Jr Doctor”],values=[“Cost”],columns=[“Drug”],aggfunc=[np.sum])pd.set_option(‘display.max_columns’,None)print(data)
Image for post

The NaN’s values can be replaced with 0 by passing fill_value argument for pivot_table() method. Run the Example 7 code and see the result.

NaN的值可以通過將ivot_table()方法的fill_value參數傳遞給0來替換。 運行示例7代碼,然后查看結果。

Example 7:

范例7:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\drugs.xlsx”)data = pd.pivot_table(df,index=[“Sr Doctor”,”Jr Doctor”],values=[“Cost”],columns=[“Drug”],aggfunc=[np.sum],fill_value=0)pd.set_option(‘display.max_columns’,None)print(data)
Image for post

Now let us add units to the values list and execute the code, run the code in Example 8 and see the result.

現在,讓我們將單元添加到值列表中并執行代碼,運行示例8中的代碼并查看結果。

Example 8:

范例8:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\drugs.xlsx”)data = pd.pivot_table(df,index=[“Sr Doctor”,”Jr Doctor”],values=[“Cost”,”Units”],columns=[“Drug”],aggfunc=[np.sum],fill_value=0)pd.set_option(‘display.max_columns’,None)print(data)
Image for post
Image for post

Now let us remove the drug from the columns and add this to the index. Run the below code in Example 9 to get a different visual representation.

現在,讓我們從列中刪除藥物并將其添加到索引中。 在示例9中運行以下代碼,以獲取不同的視覺表示。

Example 9:

范例9:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\drugs.xlsx”)data = pd.pivot_table(df,index=[“Sr Doctor”,”Jr Doctor”,”Drug”],values=[“Cost”,”Units”],aggfunc=[np.sum],fill_value=0)pd.set_option(‘display.max_columns’,None)print(data)
Image for post

To see the totals for cost and units of sum and mean, use margins=True. Run the below code and see the result.

要查看成本總計以及總和和均值的單位,請使用margins = True。 運行以下代碼,然后查看結果。

Example 10:

范例10:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\drugs.xlsx”)data = pd.pivot_table(df,index=[“Sr Doctor”,”Jr Doctor”,”Drug”],values=[“Cost”,”Units”],aggfunc=[np.sum,np.mean],fill_value=0,margins=True)pd.set_option(‘display.max_columns’,None)print(data)
Image for post

Let us now track the Sr Doctor level and track how the outcome is ordered. Run the below code of Example 11 for understanding.

現在讓我們跟蹤高級醫生的水平并跟蹤結果的排序方式。 運行以下示例11的代碼以進行理解。

Example 11:

示例11:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\Drugs.xlsx”)data = pd.pivot_table(df,index=[“Sr Doctor”,”Outcome”],values=[“Cost”],aggfunc=[np.sum],fill_value=0,margins=True)pd.set_option(‘display.max_columns’,None)print(data)
Image for post

We can also pass a dictionary to the aggfunc argument to perform different functions on each of the values which you select. Run Example 12 code and see the result.

我們還可以將字典傳遞給aggfunc參數,以對您選擇的每個值執行不同的功能。 運行示例12代碼,然后查看結果。

Example 12:

范例12:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\Drugs.xlsx”)data = pd.pivot_table(df,index=[“Sr Doctor”,”Outcome”],columns=[“Drug”],values=[“Units”,”Cost”],aggfunc={“Units”:len,”Cost”:np.sum},fill_value=0)pd.set_option(‘display.max_columns’,None)print(data)
Image for post
Image for post

We can also provide the list for each value while passing a dictionary for aggfunc argument. Run the below code in Example 13 to see the result.

我們還可以為每個值提供列表,同時傳遞aggfunc參數的字典。 在示例13中運行以下代碼以查看結果。

Example 13:

示例13:

import pandas as pdimport numpy as npdf = pd.read_excel(“C:\\Users\\admin\\Desktop\\Drugs.xlsx”)data = pd.pivot_table(df,index=[“Sr Doctor”,”Outcome”],columns=[“Drug”],values=[“Units”,”Cost”],aggfunc={“Units”:len,”Cost”:[np.sum,np.mean]},fill_value=0)pd.set_option(‘display.max_columns’,None)print(data)
Image for post
Image for post
Image for post

Thus you are able to extract and analyse the data from an excel sheet of a Microsoft product by applying the concept of a pivot table in python instead of using tools for analysis. The data analysis becomes very easy for understanding and is also very quick at processing the pivot table with python execution.

因此,您可以通過使用python中的數據透視表而不是使用分析工具來從Microsoft產品的Excel工作表中提取和分析數據。 數據分析變得非常容易理解,并且在使用python執行處理數據透視表時也非常快。

翻譯自: https://medium.com/analytics-vidhya/data-extraction-with-pivot-tables-a7d980c18dd0

數據透視表和數據交叉表

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388592.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388592.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388592.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

金融信息交換協議(FIX)v5.0

1. 什么是FIXFinancial Information eXchange(FIX)金融信息交換協議的制定是由多個致力于提升其相互間交易流程效率的金融機構和經紀商于1992年共同發起。這些企業把他們及他們的行業視為一個整體,認為能夠從對交易指示,交易指令及交易執行的高效電子數…

觀光公交

【問題描述】 風景迷人的小城 Y 市,擁有 n 個美麗的景點。由于慕名而來的游客越來越多,Y 市特意安排了一輛觀光公交車,為游客提供更便捷的交通服務。觀光公交車在第 0 分鐘出現在 1 號景點,隨后依次前往 2、3、4……n 號景點。從…

linux行命令測網速,Linux命令行測試網速的方法

最近給服務器調整了互聯網帶寬的限速策略,調到100M讓自己網站也爽一下。一般在windows上我喜歡用speedtest.net來測試,測速結果也被大家認可。在linux上speedtest.net提供了一個命令行工具speedtest-cli,用起來很方便,這里分享一下…

Delphi XE2獲取漢字拼音首字母

function CnPYIndex(const CnString: string): string;{ 返回中文的拼音首字母}const ChinaCode: array[0..25, 0..1] of Integer ((1601, 1636), (1637, 1832), (1833, 2077), (2078, 2273), (2274, 2301), (2302, 2432), (2433, 2593), (2594, 2786), (9999, 0000), …

圖像處理傅里葉變換圖像變化_傅里葉變換和圖像床單視圖。

圖像處理傅里葉變換圖像變化What do Fourier Transforms do? What do the Fourier modes represent? Why are Fourier Transforms notoriously popular for data compression? These are the questions this article aims to address using an interesting analogy to repre…

HDUOJ 1062 TEXT REVERSE

#include<iostream> #include<stdlib.h> #include <iomanip> #include<stack> using namespace std;int main(){//次數int n 0;while (cin >> n) {//這里需要讀一個字符&#xff0c;需要消除換行符的影響getchar();while (n--) {char c;stack&l…

VC++的windows服務

項目建立&#xff1a; VS2008->ATL項目->服務exe 服務部署&#xff1a; 在VS2008中新建的ATL項目&#xff0c;在網上看到的流程中都會有注冊服務這一項。 但是對服務的注冊都是只給了一個指令&#xff0c;一句話帶過&#xff0c;沒有給出具體的操作步驟 經過多次試驗…

linux中gradle編譯慢,【Linux】解決linux下android studio用gradle構建從jcenter或maven下載依賴太慢...

一個簡單的辦法&#xff0c;修改項目根目錄下的build.gradle&#xff0c;將jcenter()或者mavenCentral()替換掉即可&#xff1a;allprojects {repositories {maven{ url http://maven.oschina.net/content/groups/public/}}}但如果你有很多個項目的話&#xff0c;一個一個的改估…

MFC程序需要的函數庫及頭文件--《深入淺出MFC》

Windows程序調用的函數可分為2部分&#xff1a;C Runtimes Windows API。 C Runtimes&#xff1a; LIBC.LIB -- C Runtime函數庫的靜態鏈接版本 MSVSRT.LIB--C Runtime庫的動態鏈接版本&#xff08;如果要鏈接這一函數&#xff0c;你的程序執行時必須有MSVCRT40.DLL在場&#…

C#DNS域名解析工具(DnsLookup)

C#DNS域名解析工具(DnsLookup) DNS域名解析工具&#xff1a;DnsLookup 輸入域名后點擊Resolve按鈕即可。 主要實現代碼如下&#xff1a; private void btnResolve_Click ( object sender, EventArgs e ) {lstIPs.Items.Clear ( ); //首先把結果里的ListBox清空 try {IPHostE…

python.day05

一 字典 定義:dict, 以{},表示.每一項用逗號隔開,內部元素用key:value的形式來保存數據.例如 {"jj":"林俊杰","jay:周杰倫"} 特點:查詢效率非常高,通過key來查找元素 內部使用key來計算一個內存地址(暫時),hash算法,key必須不可變的數據類型(ke…

滯后分析rstudio_使用RStudio進行A / B測試分析

滯后分析rstudioThe purpose of this article is to provide some guide on how to conduct analysis of a sample scenario A/B test results using R, evaluate the results and draw conclusions based on the analysis.本文的目的是提供一些指南&#xff0c;說明如何使用R對…

Linux程序實現彈框,jQuery實現彈出框 效果絕對美觀

使用到JQeury寫的幾個比較好的Popup DialogBox,覺得不錯。和大家分享下。使用它們結合.net可以實現很好的效果。1.jqpopup:是個可以拖拽,縮放并可以在它上面顯示html頁面上任何一個控件組合的控件。可以和后面的主頁面通信。使用方法:先調用這幾個js文件,可以自提供的下載地址下…

Interesting visualization tools for profiling.

Interesting visualization tools for profiling. http://dtrace.org/blogs/brendan/2012/03/17/linux-kernel-performance-flame-graphs/ http://dtrace.org/blogs/brendan/2013/07/01/detecting-outliers/

MySQL的事務-原子性

MySQL的事務處理具有ACID的特性&#xff0c;即原子性&#xff08;Atomicity)、一致性&#xff08;Consistency&#xff09;、隔離性&#xff08;Isolation&#xff09;和持久性&#xff08;Durability&#xff09;。 1. 原子性指的是事務中所有操作都是原子性的&#xff0c;要…

codeforces CF438D The Child and Sequence 線段樹

$ \Rightarrow $ 戳我進CF原題 D. The Child and Sequencetime limit per test: 4 secondsmemory limit per test: 256 megabytesinput: standard inputoutput: standard outputAt the childrens day, the child came to Pickss house, and messed his house up. Picks was ang…

大型網站架構演變

今天我們來談談一個網站一般是如何一步步來構建起系統架構的&#xff0c;雖然我們希望網站一開始就能有一個很好的架構&#xff0c;但馬克思告訴我們事物是在發展中不斷前進的&#xff0c;網站架構也是隨著業務的擴大、用戶的需求不斷完善的&#xff0c;下面是一個網站架構逐步…

linux的磁盤磁頭瓷片作用,Linux 磁盤管理

硬盤物理結構以下三張圖片都是磁盤的實物圖&#xff0c;一個磁盤是由多塊堆放的瓷片組成的&#xff0c;所以磁頭的結構也是堆疊的&#xff0c;他要對每一塊瓷片進行讀取&#xff0c;磁頭是可以在不同磁道(在瓷片的表現為不同直徑的同心圓&#xff0c;磁道間是有間隔的)之間移動…

多層插件開發框架

先來幾張效果圖&#xff1a; 1.基于DATASNAP構建的中間件&#xff0c;中間件已經經過實際項目的檢驗&#xff0c;單臺中間件可支持幾千客戶端&#xff0c;中間件可集群 2.中間件支持同時連接ACCESS\SQL SERVER\MYSQL\ORACLE。。。多種數據庫系統 3.中間件同時支持TCP/IP,HTTP&a…

unity3d 可視化編程_R編程系列:R中的3D可視化

unity3d 可視化編程In the last blog, we have learned how to create “Dynamic Maps Using ggplot2“. In this article, we will explore more into the 3D visualization in R programming language by using the plot3d package.在上一個博客中&#xff0c;我們學習了如何…