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

如何将Pandas DataFrame导出为Power BI可读取的自定义列名CSV?

解决方案

完全可以实现,只需调整DataFrame的构造方式,将原字典的状态名称转为Status列,数量转为amount列,具体修改后的代码如下:

import pandas as pd

# 原始字典数据
status_data = {'AvailablePickup': 2,
               'Collected': 4,
               'Delivered': 55,
               'Totaal aantal orders': 53,
               'Packages sent out': 53,
               'Packages received by customers': 51,
               'result received': 220,
               'Test Approved': 210,
               'Test Failed': 16,
               'Not yet executed': 7}

# 构造符合需求的DataFrame
df2 = pd.DataFrame.from_dict(status_data, orient='index', columns=['amount'])
df2 = df2.reset_index().rename(columns={'index': 'Status'})

# 导出为CSV文件(index=False避免导出默认数字索引)
df2.to_csv('lolol.csv', sep=",", index=False)

关键步骤说明:

  • pd.DataFrame.from_dict(..., orient='index'):将字典的键(状态名称)作为DataFrame的索引,值(数量)作为amount列。
  • reset_index():把索引转换为普通列,再通过rename将该列命名为Status,得到两列的目标结构。
  • index=False:导出CSV时不包含默认的数字索引,确保Power BI读取到的表头就是Status和amount。

内容的提问来源于stack exchange,提问作者TimS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 20:09:30