面向Tableau開發人員的Python簡要介紹(第2部分)

用PYTHON探索數據 (EXPLORING DATA WITH PYTHON)

And we’re back! Let’s pick up where we left off in the first article of this series and use the visual we built there as a starting point.

我們回來了! 讓我們從在本系列的第一篇文章中停下來的地方開始,并以在此建立的視覺效果作為起點。

Before we dive in, let’s set the stage for what we’ll accomplish here:

在開始學習之前,讓我們為在此完成的工作做好準備:

  1. Build upon what we know about creating calculated fields in Tableau and show how that translates to Python

    在我們了解的有關在Tableau中創建計算字段的知識的基礎上,并展示如何將其轉換為Python
  2. Demonstrate how we can use color to add depth to the insights our visuals provide

    演示我們如何使用顏色來增加視覺效果所提供的見解的深度

In Tableau, you can often get your brain all twisted around a tricky situation that requires you to produce calculations various levels of aggregation. Some of you might have Googled your way through this to the point that if you type ‘level’ into the search bar, it autocompletes to ‘level of detail tableau’ (I feel you).

在Tableau中,通常會使您的大腦陷入棘手的困境,而這種棘手的情況要求您進行各種聚合級別的計算。 可能有些人用Google搜索了這一點,以至于如果您在搜索欄中輸入“ level”,它會自動完成為“ level of detail tableau”(我覺得是)。

While it will take time to learn all the nuances of Python, one of the powerful aspects of using any programming language is the freedom you have to creatively solve problems. You’ll find that in Tableau there may have been a handful of different ways to get to the correct results, while in Python (or any programming language) the options are nearly limitless.

雖然要花一些時間來學習Python的所有細微差別,但是使用任何編程語言的強大功能之一就是您可以創造性地解決問題。 您會發現,在Tableau中可能有幾種獲得正確結果的不同方法,而在Python(或任何編程語言)中,選項幾乎是無限的。

Something I think Tableau did a great job with is the marks card. That’s the area where you can drag fields to modify aspects of your visual such as size, color, labels, and details provided in your tooltip. Let’s explore how we can take something that was a click away with the marks card in Tableau, and recreate its output in Python.

我認為Tableau在標記卡方面做得很好。 在該區域中,您可以拖動字段以修改視覺效果的各個方面,例如大小,顏色,標簽和工具提示中提供的詳細信息。 讓我們探討一下如何在Tableau中使用標記卡單擊即可完成操作,然后在Python中重新創建其輸出。

In today’s exercise, we will use our ‘Profit’ and ‘Sales’ values to create a new column (similar to a calculated field) and name it ‘Profit Ratio’. We will then apply that new column to enhance the visual we created in the first article, such that the profit ratio dictates the color in the visual.

在今天的練習中,我們將使用“利潤”和“銷售”值創建一個新列(類似于計算字段)并將其命名為“利潤率”。 然后,我們將應用該新列來增強我們在第一篇文章中創建的視覺效果,從而使利潤率決定視覺效果中的顏色。

First things first, let’s build a baseline in Tableau to compare with.

首先,讓我們在Tableau中建立基線進行比較。

步驟1:通過首先在Tableau中建立目標來設定目標 (Step 1: setting a goal by building it first in Tableau)

Let’s revisit where we left off last week:

讓我們回顧上周我們停下來的地方:

Image for post
As someone who used to teach Tableau trainings, this brings back memories.
作為曾經教過Tableau培訓的人,這帶回了回憶。

Okay, so aside from removing gridlines (AKA non-data ink), what could make it better?

好吧,除了刪除網格線(又稱為非數據墨水)之外,還有什么可以使它更好呢?

We could try coloring by profitability… let’s give that a try by dragging ‘Profit’ to ‘Color’ on the marks card:

我們可以嘗試通過獲利能力進行著色……讓我們通過在標記卡上將“利潤”拖到“顏色”來進行嘗試:

Image for post
Sales by Sub-Category, colored by Profit
子類別的銷售額,按利潤劃分

Adding color gradients to visuals can be valuable. But let’s keep in mind that right now we are coloring by overall profit. If you ignore ‘Tables’, which is screaming for attention, then the color in this visual conveys an obvious message: sub-categories with higher volumes of sales also have higher volumes of profit.

為視覺效果添加顏色漸變可能很有價值。 但是請記住,現在我們正在通過整體利潤進行著色。 如果您忽略了引起人們注意的“表格”,則該視覺效果中的顏色傳達出明顯的信息:銷售量較高的子類別也具有較高的利潤量。

But what if we’re more interested in knowing which sub-category is relatively more profitable? Analyzing profitability by volume alone doesn’t provide the whole story, as you could sell one million items at 0.1% profit ratio and make more profits than selling one thousand items at 50% profit ratio. Perhaps our business could shift priorities towards products that are more profitable per unit sold if only we knew what those products were.

但是,如果我們更想知道哪個子類別相對更有利可圖,該怎么辦? 僅按數量分析獲利能力并不能提供全部信息,因為您可以以0.1%的利潤率出售一百萬件商品,并且比以50%的利潤率出售一千件商品賺更多的利潤。 如果只有我們知道這些產品是什么,也許我們的業務可以將重點轉移到每單位銷售利潤更高的產品上。

So let’s see if looking at this same data through a different lens gives us any insights.

因此,讓我們看看通過不同的眼光看待相同的數據是否能給我們帶來任何見解。

In Tableau, the calculation for profit ratio looks like this:

在Tableau中,利潤率的計算如下所示:

SUM([Profit]) / SUM([Sales])

The calculation above takes the total profit and divides that by the total sales to provide the profit ratio.

上面的計算采用總利潤,然后將其除以總銷售額即可得出利潤率。

Here’s what our visual looks like when we color by the profit ratio instead of the raw profits:

這是當我們按利潤率而不是原始利潤進行著色時,視覺效果的樣子:

Image for post
Sales by Sub-Category, colored by Profit Ratio
子類別的銷售額,按利潤率分類

The differences here are subtle, but we can immediately see that the coloration is no longer following the previous rule of ‘darker at the top, lighter at the bottom’.

這里的差異是細微的,但是我們可以立即看到,顏色不再遵循以前的規則:“頂部越暗,底部越淺”。

Paper and Labels, which were bottom feeders in our previous visual, seem to be bringing in the highest profits per sale.

紙張和標簽是我們以前的視覺效果中的最底層,似乎每次銷售帶來最高利潤。

When we color by profit ratio, we obtain a more relative picture that allows us to see that per unit of sales, paper and labels are doing well. It seems that while these products bring in lower volumes of sales, the revenue they bring in is highly profitable. Without this insight, perhaps we would neglect this profitable niche of our business.

當我們按利潤率進行著色時,我們獲得了更相關的圖像,可以使我們看到每銷售單位 ,紙張和標簽都表現良好。 這些產品雖然帶來較低的銷售量,但帶來的收益卻是高利潤的。 沒有這種洞察力,也許我們會忽略我們業務的這一有利可圖的利基市場。

步驟2:在Python中計算利潤率 (Step 2: calculating the profit ratio in Python)

No need to make this more complicated than necessary. First, let’s revisit where we left off in the last article in terms of our Python code.

無需使此操作變得不必要的復雜。 首先,讓我們重新回顧上一篇文章中關于Python代碼的內容。

We had a Pandas DataFrame named ‘subcat_sales_df’, which had columns ‘Sub-Category’, ‘Sales’, and ‘Profit’.

我們有一個名為'subcat_sales_df'的Pandas DataFrame,其中有'Sub-Category','Sales'和'Profit'列。

Here’s what it looked like:

看起來是這樣的:

Image for post

Given this DataFrame, here’s what it takes to create a new ‘Profit Ratio’ column:

給定此DataFrame,以下是創建新的“利潤率”列的步驟:

subcat_sales_df['Profit Ratio'] = subcat_sales_df['Profit'] / subcat_sales_df['Sales']

Translating that into plain English, we are defining our ‘Profit Ratio’ column to be our ‘Profit’ divided by our ‘Sales’. Nothing too crazy, right?

將其翻譯成簡單的英語,我們將“利潤率”列定義為“利潤”除以“銷售”。 沒什么太瘋狂的吧?

Keep in mind that our ‘subcat_sales_df’ DataFrame was pre-processed from our raw data in the previous article. This loops back to what we said earlier regarding freedom and flexibility in Python: you control your own destiny. Want to reshape your data and store that in a different variable like we did here? You’re completely free to do so.

請記住,我們的“ subcat_sales_df” DataFrame是根據上一篇文章中的原始數據進行預處理的。 這回溯到我們之前所說的關于Python的自由和靈活性的觀點:您可以控制自己的命運。 是否想要像我們在此處那樣重塑數據并將其存儲在其他變量中? 您完全可以這樣做。

Here’s what our ‘Profit Ratio’ looks like:

這是我們的“利潤率”:

Image for post

步驟3:在Python中使用顏色漸變進行繪圖 (Step 3: plotting with a color gradient in Python)

In our previous article, we created a bar graph showing the sales volume per sub-category.

在上一篇文章中,我們創建了一個條形圖,顯示每個子類別的銷量。

Let’s see how we can make this happen. Here’s the code I used to add ‘Profit Ratio’ as a color gradient for the chart:

讓我們看看如何實現這一目標。 這是我用來為圖表添加“利潤率”作為顏色漸變的代碼:

from matplotlib import cmfig, ax = plt.subplots(figsize=(10, 6))ax = sns.barplot(
data=subcat_sales_df,
x='Sales',
y='Sub-Category',
palette=cm.RdBu(subcat_sales_df['Profit Ratio'] * 7.5)
)ax.tick_params(axis='both', which='both', length=0)
ax.set_xlabel("Sales")
sns.despine(left=True, bottom=True)

Don’t worry about all the ‘ax’ and ‘fig’ nonsense here, this is mostly related to formatting (axis labels, etc). Feel free to play around with the scalar (7.5) seen here; adjusting it will change the nature of the color gradient.

在這里不必擔心所有的“ ax”和“ fig”廢話,這主要與格式化(軸標簽等)有關。 隨意使用此處看到的標量(7.5); 調整它會改變顏色漸變的性質。

The key here is the ‘palette’ being set to a matplotlib Colormap value. The ‘RdBu’ indicates that this color mapping runs from red to blue. The values of the color mapping are decided by the values we pass in, which in this case is the ‘Profit Ratio’ data.

這里的關鍵是將“調色板”設置為matplotlib Colormap值。 “ RdBu”表示此顏色映射從紅色到藍色。 顏色映射的值由我們傳入的值確定,在這種情況下為“利潤率”數據。

Here’s what it looks like:

看起來是這樣的:

Image for post

步驟4:調和差異 (Step 4: reconciling the differences)

You probably noticed that the coloration from Tableau and what we built here isn’t an exact match. That’s fine! In Tableau, many things are decided for you and that’s why it’s such a great click-and-drag tool. In Python, you control the details.

您可能已經注意到,Tableau的顏色與我們在此處構建的顏色并不完全匹配。 沒關系! 在Tableau中,為您決定了很多事情,這就是為什么它是一種如此出色的單擊和拖動工具的原因。 在Python中,您可以控制細節。

In the next article, we’ll take an even closer look at how those details come together with more advanced calculations! Down the road, we’ll explore joining in data from other sources.

在下一篇文章中,我們將進一步研究這些細節如何與更高級的計算結合在一起! 在未來的過程中,我們將探索如何結合其他來源的數據。

See you there!

到時候那里見!

翻譯自: https://towardsdatascience.com/a-gentle-introduction-to-python-for-tableau-developers-part-2-c18095a03e0e

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

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

相關文章

GAC中的所有的Assembly都會存放在系統目錄%winroot%/assembly下面

是的,GAC中的所有的Assembly都會存放在系統目錄"%winroot%/assembly下面。放在系統目錄下的好處之一是可以讓系統管理員通過用戶權限來控制Assembly的訪問。 關于GAC本身,上面redcaff_l所引述的一段話正是MSDN中對GAC的定義。GAC全稱是Global A…

Mysql(三) Mysq慢查詢日志

Mysql Slow Query Log MYSQL慢查詢日志是用來記錄執行時間超過指定時間的查詢語句。通過慢查詢日志,可以查找出哪些查詢語句的執行效率很低,以便進行優化。一般建議開啟,它對服務器性能的影響微乎其微,但是可以記錄mysql服務器上執…

繪制基礎知識-canvas paint

先來看一下Canvas Canvas 用來提供draw方法的調用。繪制東西需要4個基本的組建:一個bitmap用來存放像素,一個canvas用來提供draw方法的調用(往bitmap里寫入),原始繪制元素(e.g.Rect, Path, text,Bitmap), 一個paint。 …

Python - 調試Python代碼的方法

調試(debug) 將可疑環節的變量逐步打印出來,從而檢查哪里是否有錯。讓程序一部分一部分地運行起來。從核心功能開始,寫一點,運行一點,再修改一點。利用工具,例如一些IDE中的調試功能,提高調試效率。Python …

設計組合中的10個嚴重錯誤可能會導致您喪命

As an agency co-founder and design lead, I’ve been participating in many recruitment processes. I’ve seen hundreds of portfolios and CVs of aspiring designers. If you’re applying for a UI designer position, it is good to have some things in mind and to …

netflix_Netflix的計算因果推論

netflixJeffrey Wong, Colin McFarland杰弗里黃 , 科林麥克法蘭 Every Netflix data scientist, whether their background is from biology, psychology, physics, economics, math, statistics, or biostatistics, has made meaningful contributions to the way…

算法題庫網站

Google Code Jam(GCJ)Peking University Online Judge(POJ)CodeForces(CF)LeetCode(LC)Aizu Online Judge(AOJ)

org.dom4j.DocumentException: null Nested exception: null解決方法

由于最近在學習使用Spring架構,經常會遇到與xml文檔打交道,今天遇到了此問題,特來分享一下解決方案。 出錯原因: 很明顯是因為找不到文件路徑。這個原因是因為我使用了*.clas.getResourceAsStream(xmlFilePath&#xf…

MySQL命令學習

上面兩篇博客講了MySQL的安裝、登錄,密碼重置,為接下來的MySQL命令學習做好了準備,現在開啟MySQL命令學習之旅吧。 首先打開CMD,輸入命令:mysql -u root -p 登錄MySQL。 注意:MySQL命令終止符為分號 (;) …

實驗心得_大腸桿菌原核表達實驗心得(上篇)

大腸桿菌原核表達實驗心得(上篇)對于大腸桿菌蛋白表達,大部分小伙伴都覺得 so easy! 做大腸桿菌蛋白表達十幾年經歷的老司機還經常陰溝翻船,被大腸桿菌表達蛋白虐千百遍的慘痛經歷,很多小伙伴都有切膚之痛。福因德接下…

scrapy從安裝到爬取煎蛋網圖片

下載地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/pip install wheelpip install lxmlpip install pyopensslpip install Twistedpip install pywin32pip install scrapy scrapy startproject jandan 創建項目cd jandancd jandan items.py 存放數據pipelines.p…

高斯金字塔 拉普拉斯金字塔_金字塔學入門指南

高斯金字塔 拉普拉斯金字塔The topic for today is on data validation and settings management using Python type hinting. We are going to use a Python package called pydantic which enforces type hints at runtime. It provides user-friendly errors, allowing you …

基本排序算法

插入排序 基本思想&#xff1a;把待排序列表分為已排和未排序兩部分&#xff0c;從未排序左邊取值&#xff0c;按順序從已排序的右端開始對比插入到相應的位置。 java代碼實現 private void insertSort(int[] arr){int i, j;int temp;for(i 0; i < arr.length; i){temp …

自定義版本更新彈窗

目錄介紹 1.Animation和Animator區別 2.Animation運行原理和源碼分析 2.1 基本屬性介紹2.2 如何計算動畫數據2.3 什么是動畫更新函數2.4 動畫數據如何存儲2.5 Animation的調用 3.Animator運行原理和源碼分析 3.1 屬性動畫的基本屬性3.2 屬性動畫新的概念3.3 PropertyValuesHold…

《SQL Server 2008從入門到精通》--20180716

1.鎖 當多個用戶同時對同一個數據進行修改時會產生并發問題&#xff0c;使用事務就可以解決這個問題。但是為了防止其他用戶修改另一個還沒完成的事務中的數據&#xff0c;就需要在事務中用到鎖。 SQL Server 2008提供了多種鎖模式&#xff1a;排他鎖&#xff0c;共享鎖&#x…

googleearthpro打開沒有地球_嫦娥五號成功著陸地球!為何嫦娥五號返回時會燃燒,升空卻不會?...

目前&#xff0c;嫦娥五號已經帶著月壤成功降落到地球上&#xff0c;創造了中國航天的又一里程碑。嫦娥五號這一路走來&#xff0c;困難重重&#xff0c;但都被我國航天科技人員逐一克服&#xff0c;最終圓滿地完成了嫦娥五號的月球采樣返回地球任務。嫦娥五號最后這一步走得可…

語言認知偏差_我們的認知偏差正在破壞患者的結果數據

語言認知偏差How do we know if we are providing high-quality care? The answer to this question is sought by a multitude of parties: patients, clinicians, educators, legislators, and insurance companies. Unfortunately, it’s not easy to determine. There is …

android 打包相關問題記錄

Android 中的打包配置在build.gradle文件中&#xff0c;下面對該文件的內容做一下記錄。 buildscript {repositories {jcenter()}dependencies {classpath com.android.tools.build:gradle:2.2.0} } 這里生命了倉庫的位置&#xff0c;依賴gradle的版本。 android{} android {…

本文將引導你使用XNA Game Studio Express一步一步地創建一個簡單的游戲

本文將引導你使用XNA Game Studio Express一步一步地創建一個簡單的游戲 第1步: 安裝軟件 第2步: 創建新項目 第3步: 查看代碼 第4步: 加入一個精靈 第5步: 使精靈可以移動和彈跳 第6步: 繼續嘗試! 完整的實例 第1步: 安裝軟件在動手之前,先確定你已經安裝了所需的軟件,其中包…

C#中實現對象的深拷貝

深度拷貝指的是將一個引用類型&#xff08;包含該類型里的引用類型&#xff09;拷貝一份(在內存中完完全全是兩個對象&#xff0c;沒有任何引用關系)..........  直接上代碼&#xff1a; 1 /// <summary>2 /// 對象的深度拷貝&#xff08;序列化的方式&#xf…