我有一个关于Power BI数据集的问题,在过go 的一个月里我无法解决这个问题,所以在下面的图片中,您可以看到我的步骤编号.

在第一步中,您可以看到我的数据源在一个Excel文件中. 在第二步到第五步中,您可以看到我在POWER查询中的步骤,该步骤涉及将每列转换为文本、在POWER查询中运行Python以填充数据集数据框、获取文本格式的数据集,以及再次将所有内容更改为数据的最后一次转换.

我的问题在图6和图7中可见. 在图6中,我得到的是ISO 8601格式,但无法将其转换为日期(例如,我try 了pd.to_DateTime或Datetime.Date.FromisoFormat)

在图7中,这个问题变得更糟.我不仅在转换方面有问题,而且我还不时地运行NAT和其他问题,但什么都不起作用

此外,我在步骤2中将每一列都更改为文本的原因是因为我在这里读到了:Python script in Power BI returns date as Microsoft.OleDb.Date

所以如果你能帮我这个忙我会很感激的.我不知道怎么做那些情节,因为我总是遇到各种各样的错误

此外,以下是代码

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

# dataset = pandas.DataFrame(Scale, Y 1, Y 2, Y 3, Y 4, Y 5, Y 6)
# dataset = dataset.drop_duplicates()

# Paste or type your script code here:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime

plt.figure(figsize=(15, 16))

# In below plot everything is text (ISO 8601 format) and not date
#plt.plot(dataset['Scale'],dataset['Scale'])

#Below line does not create any plot at all
plt.plot(dataset['Y 1'],dataset['Y 1'])

plt.show()

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

推荐答案

  • 我认为让 playbook 来做所有的工作会更好.因此,使用pandas管理数据转换和绘图.
  • 数据视图中,将所有列设置为Text
  • Load dataset into pandas with df = pd.DataFrame(dataset)
    • pd.to_datetime to format the columns. To format multiple columns with the same format, use .apply, which is vectorized for each column
      • 单列为df['col'] = pd.to_datetime(df['col'], format='...').
      • 请注意,pandas根据数据的时间范围以不同的方式格式化xtick标签.有许多现有的问题,因此,处理格式的外观和频率的扁虱和标签.
      • answer表明,当列不包含时间组件时,使用.dt.date将导致Plot API将Date xtick标签居中.但是,在Power BI中,结果是TypeError: no numeric data to plot,因此使用代码块中的一个选项将xtick标签居中.
    • pd.DataFrame.plot可直接绘制数据帧.这使用matplotlib作为默认后端.
  • Tested with 100, 101, 102, 103

在Python脚本编辑器中

import pandas as pd
import matplotlib.pyplot as plt

# load the data into a dataframe
df = pd.DataFrame(dataset)

# format all of the columns, specifying the format
df = df.apply(lambda col: pd.to_datetime(col, format='%m/%d/%Y'), axis=0)

# plot the dataframe directly with pandas.DataFrame.plot
# use the y= parameter to set specific columns instead of all columns, e.g. y=['Y1']
ax = df.plot(x='Scale', title='In 数据视图, set each column as Text', figsize=(12, 7), rot=0)

# change the tick label to center aligned
for tick in ax.xaxis.get_major_ticks():
    tick.label1.set_horizontalalignment('center')

# optionally, this can be used to center the labels instead
# plt.setp(ax.get_xticklabels(), ha="center")

# show the plot
plt.show()

数据视图

enter image description here

报告视图

Python脚本编辑器

enter image description here

可视化

enter image description here

Python相关问答推荐

剧作家Python没有得到回应

如何使用SubProcess/Shell从Python脚本中调用具有几个带有html标签的参数的Perl脚本?

如何让 turtle 通过点击和拖动来绘制?

线性模型PanelOLS和statmodels OLS之间的区别

替换字符串中的多个重叠子字符串

为什么tkinter框架没有被隐藏?

如何使用Python将工作表从一个Excel工作簿复制粘贴到另一个工作簿?

ModuleNotFound错误:没有名为flags.State的模块; flags不是包

从numpy数组和参数创建收件箱

当从Docker的--env-file参数读取Python中的环境变量时,每个\n都会添加一个\'.如何没有额外的?

numpy卷积与有效

pandas在第1列的id,第2列的标题,第3列的值,第3列的值?

Pandas Loc Select 到NaN和值列表

从Windows Python脚本在WSL上运行Linux应用程序

从一个df列提取单词,分配给另一个列

我怎么才能用拉夫分拣呢?

TypeError:';Locator';对象无法在PlayWriter中使用.first()调用

随机森林n_估计器的计算

对于标准的原始类型注释,从键入`和`从www.example.com `?

类型对象';敌人';没有属性';损害';