You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Power BI Python视觉对象生成相关系数表全为NaN的问题排查及代码适配请求

Power BI Python视觉对象生成相关系数表全为NaN的问题排查及代码适配请求

我现在在Power BI里用Matplotlib做Python视觉对象,想生成一个相关系数表,但代码运行后出来的全是NaN值(附:生成的视觉对象全为NaN的截图),这可愁坏我了。

我之前对数据做了不少转换——比如按特定年份筛选,还按CIP Family这个变量做了聚合。我想知道这段代码能不能在Power BI里正常运行,如果可以的话该怎么调整?

import pandas as pd
import matplotlib.pyplot as plt 

dataset['Date'] = pd.to_datetime(dataset['Date'], errors='coerce')

# 1. Filter and group data for CTOTALTforFinal99Family (2010-2019)
filtered_CTOTALTforFinal99Family = dataset[
    (dataset['Date'] >= '2010-01-01') & (dataset['Date'] <= '2019-01-01')
].groupby('CIP Family').agg({'CTOTALTforFinal99Family': 'sum'}).reset_index()

# 2. Filter and group data for CTOTALTNew2020Family (2020-2023)
filtered_CTOTALTNew2020Family = dataset[
    (dataset['Date'] >= '2020-01-01') & (dataset['Date'] <= '2023-01-01')
].groupby('CIP Family').agg({'CTOTALTNew2020Family': 'sum'}).reset_index()

# 3. Group and sum New2020CountFamily by 'CIP Family'
grouped_New2020CountFamily = dataset.groupby('CIP Family').agg({'New2020CountFamily': 'sum'}).reset_index()

# 4. Calculate PerctCTOTALT99Family for each 'CIP Family' (2010-2019)

filtered_2010_2019 = dataset[(dataset['Date'] >= '2010-01-01') & (dataset['Date'] <= '2019-01-01')]

grouped_2010_2019 = filtered_2010_2019.groupby('CIP Family').agg({
    'CTOTALTforFinal99Family': 'sum',
    'CTOTALTFamily': 'sum'
}).reset_index()

grouped_2010_2019['PerctCTOTALT99Family'] = (
    grouped_2010_2019['CTOTALTforFinal99Family'] / grouped_2010_2019['CTOTALTFamily']
) * 100
grouped_2010_2019['PerctCTOTALT99Family'] = grouped_2010_2019['PerctCTOTALT99Family'].fillna(0)

 # 5. Calculate PerctCTOTALTNew2020Family for each 'CIP Family' (2020-2023)
filtered_2020_2023 = dataset[(dataset['Date'] >= '2020-01-01') & (dataset['Date'] <= '2023-01-01')]
grouped_2020_2023 = filtered_2020_2023.groupby('CIP Family').agg({
    'CTOTALTNew2020Family': 'sum',
    'CTOTALTFamily': 'sum'
}).reset_index()

grouped_2020_2023['PerctCTOTALTNew2020Family'] = (
    grouped_2020_2023['CTOTALTNew2020Family'] / grouped_2020_2023['CTOTALTFamily']
) * 100
grouped_2020_2023['PerctCTOTALTNew2020Family'] = grouped_2020_2023['PerctCTOTALTNew2020Family'].fillna(0)

# Merge all variables of interest by 'CIP Family'
merged_data = (
    grouped_2010_2019[['CIP Family', 'CTOTALTforFinal99Family', 'PerctCTOTALT99Family']]
    .merge(grouped_2020_2023[['CIP Family', 'CTOTALTNew2020Family', 'PerctCTOTALTNew2020Family']], on='CIP Family', how='outer')
    .merge(grouped_New2020CountFamily, on='CIP Family', how='outer')
)

# Final Column Selection and Ordering
final_data = merged_data[[
    'CIP Family',
    'CTOTALTforFinal99Family',
    'PerctCTOTALT99Family',
    'CTOTALTNew2020Family',
    'PerctCTOTALTNew2020Family',
    'New2020CountFamily'
]]

# Compute the correlation matrix
correlation_data = final_data.drop(columns='CIP Family').copy()
corr = final_data.corr().round(3)

# Create a new figure for displaying the table
fig, ax = plt.subplots(figsize=(12, 6))  # Adjust figure size

# Hide the axes (only show the table)
ax.axis('off')

# Display the correlation matrix as a table
table = ax.table(
    cellText=corr.values,
    colLabels=corr.columns,
    rowLabels=corr.index,
    loc='center',
    cellLoc='center',
    bbox=[0, 0, 1, 1],  # Table occupies full figure area
)

# Customize table header and styling
table.auto_set_font_size(False)
table.set_fontsize(10)  # Adjust font size as needed

# Bold the header row and column
for (i, j), cell in table.get_celld().items():
    if i == 0 or j == -1:
        cell.set_text_props(weight='bold')

# Add a title for clarity
plt.title("Correlation Matrix", fontsize=16, weight='bold')

# Show the table in Power BI
plt.show()

顺带一提,我把代码稍微改改在Jupyter Notebook里跑是完全正常的。我怀疑是适配Power BI的时候哪里弄错了,或者代码里有Power BI Python视觉对象不支持的写法?我已经跟ChatGPT折腾好久了还是找不到问题,真的搞不懂Power BI的Python视觉对象到底有啥门道。

备注:内容来源于stack exchange,提问作者ZZplantzzz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 17:28:00