問題描述
截至 2018 年 8 月 9 日,Power BI 支持 Python 可視化.他們以前支持 R 可視化,但我仍然覺得這些集成有點尷尬.讓我告訴你我的意思:
<小時>假設您有一個包含時間序列數據的表,其中第一行包含名稱日期"和值",內容分別是格式為 yyyy-mm-dd 的日期和一個數字:
日期、數值2017-01-12,12017-01-13,42017-01-14,22017-01-15,42017-01-16,22017-01-17,22017-01-18,22017-01-19,52017-01-20,52017-01-21,52017-01-22,52017-01-23,62017-01-24,32017-01-25,62017-01-26,62017-01-27,52017-01-28,82017-01-29,42017-01-30,2
如果您將該數據集存儲為像 timerseries.csv
這樣的文本文件并使用 Get Data | 導入它.文本/CSV,你會得到一個比 VISUALIZATIONS |字段,像這樣:
您可以使用 VISUALIZATIONS | 檢查您的表表 并獲取:
有了這個設置,你應該認為你已經準備好使用這個漂亮的新功能來釋放 Py VISUALIZATION 的力量了:
如果你點擊它,你會得到這個:
你被告知
<塊引用>將字段拖入可視化"窗格中的值"區域以開始腳本
如果你從 Value
開始,你會在編輯器中得到這個默認設置:
如果您按照 Power BI 團隊在
但這就是我目前的結局.
如果編輯器中的默認數據框共享標準數據框的功能,您應該能夠引用該數據框中的列并使用此代碼段輕松繪制圖表:
import matplotlib.pyplot as pltplt.plot(數據集['值'])plt.show()
但是當你運行它時,它只會返回一個錯誤:
至少可以說細節很詳細.
我也嘗試過同時導入 Dates
和 Values
,并嘗試使用 dataset.plot()
,但似乎沒有任何效果.我還嘗試通過這種方式將日期層次結構分解為簡單的日期:
那么,對數據格式、導入方法和/或代碼片段有什么想法嗎?
感謝您的任何建議!
編輯 1 - 按照 Foxan Ng 的回答:
在值"字段中添加兩列:
這仍然返回一個錯誤
<塊引用>TypeError: from_bounds() 接受 4 個位置參數,但給出了 6 個
我沒有遇到你提到的錯誤.您是否將兩列都放入 Values
?
import matplotlib.pyplot as pltplt.plot(數據集['日期'],數據集['值'])plt.show()
<小時>使用 M 查詢更新:
讓Source = Csv.Document(File.Contents("C:your-directory.. imerseries.csv"),[Delimiter=",", Columns=2, Encoding=1252, QuoteStyle=QuoteStyle.None]),#"PromoteHeaders" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Value", Int64.Type}})在#改變類型"
As of today, August 9 2018, Power BI supports Python Visualizations. They've had support for R Visualizations before, but I still find these integrations to be a bit awkward. Let me show you what I mean:
Let's say that you have a table with time series data, where the top row containts the names 'Date' and 'Value', and the contents are dates of the form yyyy-mm-dd and a number, respectively:
Date,Value
2017-01-12,1
2017-01-13,4
2017-01-14,2
2017-01-15,4
2017-01-16,2
2017-01-17,2
2017-01-18,2
2017-01-19,5
2017-01-20,5
2017-01-21,5
2017-01-22,5
2017-01-23,6
2017-01-24,3
2017-01-25,6
2017-01-26,6
2017-01-27,5
2017-01-28,8
2017-01-29,4
2017-01-30,2
If you store that dataset as a textfile like timerseries.csv
and import it using Get Data | Text/CSV, you get a table uner VISUALIZATIONS | FIELDS, like this:
You can inspect your table using VISUALIZATIONS | Table and get:
With this setup, one should think that you were all set for unleashing the power of a Py VISUALIZATION using this beautiful new feature:
If you click that, you get this:
And you're told to
Drag fields into the Values area in the Visualization pane to start scripting
If you start with Value
, you get this default setup in the editor:
And if you follow the instructions given by the Power BI team in the August 2018 feature summary you should be able to make a matplotlib plot quite easily.
But this is where it ends for me at the time being.
If the default dataframe in the editor shares the features of a standard dataframe, you should be able to reference a column in that dataframe and easily make a plot with this snippet:
import matplotlib.pyplot as plt
plt.plot(dataset['Value'])
plt.show()
But when you run it, it onlu returns an error:
And the details are elaborate to say the least.
I've also tried to import both Dates
and Values
, and I've tried plotting the dataframe directly with dataset.plot()
, but nothing seems to be working. I've also tried stripping the date hierarchy down to simple dates this way:
So, any ideas on the dataformat, import method and/or the snippet?
Thank you for any suggestions!
EDIT 1 - Following the answer from Foxan Ng:
Add both columns in the Value field:
This still returns an error edning with:
TypeError: from_bounds() takes 4 positional arguments but 6 were given
I didn't encounter errors that you've mentioned. Have you dropped in both columns into Values
?
import matplotlib.pyplot as plt
plt.plot(dataset['Date'], dataset['Value'])
plt.show()
UPDATED with M query:
let
Source = Csv.Document(File.Contents("C:your-directory.. imerseries.csv"),[Delimiter=",", Columns=2, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Value", Int64.Type}})
in
#"Changed Type"
這篇關于Power BI 中 Python 可視化中時間序列的最佳數據格式是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!