在 Power BI 中使用 python 脚本操作 SQL 数据帧

Posted

技术标签:

【中文标题】在 Power BI 中使用 python 脚本操作 SQL 数据帧【英文标题】:Manipulate SQL dataframe with python script in Power BI 【发布时间】:2020-10-02 16:27:55 【问题描述】:

我想在 Power BI 中对 SQL 数据框执行一个简单的 Python 脚本。 但错误似乎表明 SQL 表已被读取为 CSV 文件,我不知道为什么脚本将数据帧视为 CSV 文件而不是 SQL 数据帧。

python脚本是:import pandas as pd dataset['COD-MARQ'] = dataset['COD-MARQ'].str.strip()

关于我应该如何处理的任何想法?

谢谢

Traceback (most recent call last):
  File "PythonScriptWrapper.PY", line 7, in <module>
    df1 = pandas.read_csv('input_df_da064532-6620-4e48-a091-ff580b127759.csv')
  File "C:\Users\afalieres\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 686, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\afalieres\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 458, in _read
    data = parser.read(nrows)
  File "C:\Users\afalieres\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 1186, in read
    ret = self._engine.read(nrows)
  File "C:\Users\afalieres\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 2145, in read
    data = self._reader.read(nrows)
  File "pandas\_libs\parsers.pyx", line 847, in pandas._libs.parsers.TextReader.read
  File "pandas\_libs\parsers.pyx", line 862, in pandas._libs.parsers.TextReader._read_low_memory
  File "pandas\_libs\parsers.pyx", line 941, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 1073, in pandas._libs.parsers.TextReader._convert_column_data
  File "pandas\_libs\parsers.pyx", line 1119, in pandas._libs.parsers.TextReader._convert_tokens
  File "pandas\_libs\parsers.pyx", line 1194, in pandas._libs.parsers.TextReader._convert_with_dtype
  File "pandas\_libs\parsers.pyx", line 1814, in pandas._libs.parsers._try_int64
MemoryError: Unable to allocate 64.0 KiB for an array with shape (8192,) and data type int64

Détails :
    DataSourceKind=Python
    DataSourcePath=Python
    Message=Ρŷтнőŋ şсŗĩрţ εггǿŗ.
Traceback (most recent call last):
  File "PythonScriptWrapper.PY", line 7, in <module>
    df1 = pandas.read_csv('input_df_da064532-6620-4e48-a091-ff580b127759.csv')
  File "C:\Users\afalieres\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 686, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\afalieres\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 458, in _read
    data = parser.read(nrows)
  File "C:\Users\afalieres\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 1186, in read
    ret = self._engine.read(nrows)
  File "C:\Users\afalieres\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 2145, in read
    data = self._reader.read(nrows)
  File "pandas\_libs\parsers.pyx", line 847, in pandas._libs.parsers.TextReader.read
  File "pandas\_libs\parsers.pyx", line 862, in pandas._libs.parsers.Tex...
    ErrorCode=-2147467259
    ExceptionType=Microsoft.PowerBI.Scripting.Python.Exceptions.PythonScriptRuntimeException ```

【问题讨论】:

你能把最后几行回溯贴在容易出现错误的地方吗? 感谢您的回复!我用整个错误消息更新了帖子。希望对你有帮助 【参考方案1】:

我不肯定这是问题所在,但在我看来 dataset 指的是上一步而不是原始源,这意味着它不再采用 SQL 数据帧格式。您可能想要使用 python 导入原始源,或者修改您的脚本以将数据集视为不是 SQL 数据框,而是以查询编辑器传递给 python 脚本的任何格式(我认为熊猫数据框)。

另外说明,在这种特殊情况下,似乎没有必要使用 python 进行简单的转换,而这可以在 M 中以本机方式轻松完成。

【讨论】:

感谢您的回复!我是 Power BI 的新手。原始来源是一个 SQL 数据框,我想在其上应用一个更大的 Python 脚本。这条线只是一个测试。我导入了 SQL 数据框,点击“获取数据”,然后点击“SQL Azure 数据库”

以上是关于在 Power BI 中使用 python 脚本操作 SQL 数据帧的主要内容,如果未能解决你的问题,请参考以下文章

在 Power BI 中使用 python 脚本操作 SQL 数据帧

使用 Python 在 Power BI 中过滤 Python Script Visual 中的数据

Power BI:如何在 Power Query 编辑器中将 Python 与多个表一起使用?

Power BI 中的 Python 脚本将日期返回为 Microsoft.OleDb.Date

Power BI:多个表作为 Power Query 中 Python 的输出

使用 Python 获取文件夹中的数据以向 Power BI 提供 excel 文件