日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

面向Tableau开发人员的Python简要介绍(第2部分)

發(fā)布時(shí)間:2023/11/29 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 面向Tableau开发人员的Python简要介绍(第2部分) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

用PYTHON探索數(shù)據(jù) (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.

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

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

在開始學(xué)習(xí)之前,讓我們?yōu)樵诖送瓿傻墓ぷ髯龊脺?zhǔn)備:

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

    在我們了解的有關(guān)在Tableau中創(chuàng)建計(jì)算字段的知識的基礎(chǔ)上,并展示如何將其轉(zhuǎn)換為Python
  • Demonstrate how we can use color to add depth to the insights our visuals provide

    演示我們?nèi)绾问褂妙伾珌碓黾右曈X效果所提供的見解的深度
  • 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中,通常會(huì)使您的大腦陷入棘手的困境,而這種棘手的情況要求您進(jìn)行各種聚合級別的計(jì)算。 可能有些人用Google搜索了這一點(diǎn),以至于如果您在搜索欄中輸入“ level”,它會(huì)自動(dòng)完成為“ 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.

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

    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.

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

    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.

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

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

    首先,讓我們在Tableau中建立基線進(jìn)行比較。

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

    Let’s revisit where we left off last week:

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

    As someone who used to teach Tableau trainings, this brings back memories.作為曾經(jīng)教過Tableau培訓(xùn)的人,這帶回了回憶。

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

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

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

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

    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.

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

    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.

    但是,如果我們更想知道哪個(gè)子類別相對更有利可圖,該怎么辦? 僅按數(shù)量分析獲利能力并不能提供全部信息,因?yàn)槟梢砸?.1%的利潤率出售一百萬件商品,并且比以50%的利潤率出售一千件商品賺更多的利潤。 如果只有我們知道這些產(chǎn)品是什么,也許我們的業(yè)務(wù)可以將重點(diǎn)轉(zhuǎn)移到每單位銷售利潤更高的產(chǎn)品上。

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

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

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

    在Tableau中,利潤率的計(jì)算如下所示:

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

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

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

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

    這是當(dāng)我們按利潤率而不是原始利潤進(jìn)行著色時(shí),視覺效果的樣子:

    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’.

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

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

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

    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.

    當(dāng)我們按利潤率進(jìn)行著色時(shí),我們獲得了更相關(guān)的圖像,可以使我們看到每銷售單位 ,紙張和標(biāo)簽都表現(xiàn)良好。 這些產(chǎn)品雖然帶來較低的銷售量,但帶來的收益卻是高利潤的。 沒有這種洞察力,也許我們會(huì)忽略我們業(yè)務(wù)的這一有利可圖的利基市場。

    步驟2:在Python中計(jì)算利潤率 (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.

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

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

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

    Here’s what it looked like:

    看起來是這樣的:

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

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

    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是根據(jù)上一篇文章中的原始數(shù)據(jù)進(jìn)行預(yù)處理的。 這回溯到我們之前所說的關(guān)于Python的自由和靈活性的觀點(diǎn):您可以控制自己的命運(yùn)。 是否想要像我們在此處那樣重塑數(shù)據(jù)并將其存儲在其他變量中? 您完全可以這樣做。

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

    這是我們的“利潤率”:

    步驟3:在Python中使用顏色漸變進(jìn)行繪圖 (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.

    在上一篇文章中,我們創(chuàng)建了一個(gè)條形圖,顯示每個(gè)子類別的銷量。

    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:

    讓我們看看如何實(shí)現(xiàn)這一目標(biāo)。 這是我用來為圖表添加“利潤率”作為顏色漸變的代碼:

    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 ‘a(chǎn)x’ 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.

    在這里不必?fù)?dān)心所有的“ ax”和“ fig”廢話,這主要與格式化(軸標(biāo)簽等)有關(guān)。 隨意使用此處看到的標(biāo)量(7.5); 調(diào)整它會(huì)改變顏色漸變的性質(zhì)。

    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.

    這里的關(guān)鍵是將“調(diào)色板”設(shè)置為matplotlib Colormap值。 “ RdBu”表示此顏色映射從紅色到藍(lán)色。 顏色映射的值由我們傳入的值確定,在這種情況下為“利潤率”數(shù)據(jù)。

    Here’s what it looks like:

    看起來是這樣的:

    步驟4:調(diào)和差異 (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.

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

    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.

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

    See you there!

    到時(shí)候那里見!

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

    總結(jié)

    以上是生活随笔為你收集整理的面向Tableau开发人员的Python简要介绍(第2部分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。