最新消息:20210917 已从crifan.com换到crifan.org

【部分解决】pandas输出excel报错:If using all scalar values, you must pass an index

values crifan 375浏览 0评论
折腾:
【未解决】pandas输出数据到excel时如何输出特定格式的第一行
期间,用代码:
personList = list(teamMoneySumDict.keys())
moneyList = list(teamMoneySumDict.values())


personList.insert(0, "销售员账户")
moneyList.insert(0, "团队当前业绩")


print("firstTime=%s, lastTime=%s" % (firstTime, lastTime))


beginYear = firstTime.year
beginMonth = firstTime.month
beginDay = firstTime.day
beginHour = firstTime.hour
beginMinute = firstTime.minute


endYear = lastTime.year
endMonth = lastTime.month
endDay = lastTime.day
endHour = lastTime.hour
endMinute = lastTime.minute


summaryData = {
  # "销售员账户": personList,
  # "团队当前业绩": moneyList,
  beginYear: personList,
  "年": moneyList,
  beginMonth: "",
  "月": "",
  beginDay: "",
  "日": "",
  beginHour: "",
  "时": "",
  beginMinute: "",
  "分": "",
  "--": "",
  endYear: "",
  "年": "",
  endMonth: "",
  "月": "",
  endDay: "",
  "日": "",
  endHour: "",
  "时": "",
  endMinute: "",
  "分": "",
}


outputDf = pd.DataFrame(data=summaryData)


outputDf.to_excel(OutputFullPath, sheet_name="统计结果")
结果:
竟然报错了:
发生异常: ValueError
If using all scalar values, you must pass an index
If using all scalar values, you must pass an index
Python常见错误:ValueError: If using all scalar values, you must pass an index(四种解决方案)_小白白的博客-CSDN博客
data = pd.DataFrame(dict,index=[0])
python – Constructing pandas DataFrame from values in variables gives “ValueError: If using all scalar values, you must pass an index” – Stack Overflow
好像是:
此处 需要list,但是传入了 “” 空字符串
所以应该改为:空列表?
去试试:
summaryData = {
  beginYear: personList,
  "年": moneyList,

  beginMonth: [],
  "月": [],
  beginDay: [],
  "日": [],
  beginHour: [],
  "时": [],
  beginMinute: [],
  "分": [],
  "--": [],
  endYear: [],
  "年": [],
  endMonth: [],
  "月": [],
  endDay: [],
  "日": [],
  endHour: [],
  "时": [],
  endMinute: [],
  "分": [],
}
结果:
就没报错了。
【总结】
此处用 代码:
summaryData = {
  beginYear: personList,
  "年": moneyList,
  beginMonth: "",
  "月": "",
}

outputDf = pd.DataFrame(data=summaryData)
会报错:
If using all scalar values, you must pass an index
原因:
传给DataFrame的dict中,应该是:
str: list
但是却包含了:
str: str
其中value值是str,属于scalar values 是不对的。
解决办法:
把value改为list即可
summaryData = {
  beginYear: personList,
  "年": moneyList,
  beginMonth: [],
  "月": [],
}


outputDf = pd.DataFrame(data=summaryData)
后记:
但是输出的内容,不是我们希望的:
结果不对。
那去改为:

  "年": moneyList,


  beginMonth: "",
  "月": "",
  beginDay: "",
。。。

outputDf = pd.DataFrame(data=summaryData, index=[0])
结果:
也没报错
但是结果还不是我们希望的。
python – Dictionary to Dataframe Error: “If using all scalar values, you must pass an index” – Stack Overflow
python – pandas read_json: “If using all scalar values, you must pass an index” – Stack Overflow
pandas – Convert Python dict into a dataframe – Stack Overflow

转载请注明:在路上 » 【部分解决】pandas输出excel报错:If using all scalar values, you must pass an index

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
85 queries in 0.122 seconds, using 21.59MB memory