【问题标题】:Import Data frame value to Dictionary将数据框值导入字典
【发布时间】:2020-10-24 01:37:47
【问题描述】:

我有一个从 pdf 中提取表格数据的代码,我想将数据框转换为字典,如果我在 tabula 中提到输出为 JSON,它也会给出不需要的坐标。我只想要表中存在的数据。如果把数据框转成字典,我可以继续做其他的处理工作

from tabula import read_pdf
from tabulate import tabulate
import pandas as pd

df = read_pdf("http://www.uncledavesenterprise.com/file/health/Food%20Calories%20List.pdf",multiple_tables=True,pages='3' ,pandas_options={'header':None},guess = False)
print (df)

【问题讨论】:

  • 您可以使用 df.to_dict() 或 df.to_dict(orient='records')
  • @PramoteKuacharoen 它给了我这个错误 AttributeError: 'list' object has no attribute 'to_json'
  • 你必须在你的数据框上做,而不是一个列表。

标签: python tabula


【解决方案1】:

您可以使用df.to_dict() 将数据框转换为字典。您可以直接使用此方法,也可以提供一些参数,可以检查 here for pandas v1.0.5 或 here for pandas v0.23.4 。您还可以使用 df.to_json() 将数据帧转换为 JSON。有关参数的信息可以分别在 v1.0.5 和 v0.23.4 的 herehere 中找到。

【讨论】:

  • @devShaurya 它给了我这个错误 AttributeError: 'list' object has no attribute 'to_json'
  • @ChirstyCR_007 首先使用 pd.DataFrame(df) 将 df 变量转换为 pandas 数据帧。您可以在 pandas 文档中查看相关信息。然后用我的答案。
猜你喜欢
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 2016-10-15
  • 2023-03-22
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 2021-12-08
相关资源
最近更新 更多