【问题标题】:Get excel column into a variable将excel列转换为变量
【发布时间】:2020-11-02 14:48:29
【问题描述】:

我想将一些 xls 数据移动到 json 中。我不能只使用现成的解决方案,因为这有点特殊。

这是excel

代码如下:

import pandas

xl = pandas.ExcelFile("./data/file.xlsx")
df = xl.parse("2")
x = df["XX"][0]
print(x)

# writing to file
text_file = open("json_files/Output.json", "w")

# text_file.write(json_str)

text_file.close()

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Users\aironsid\Documents\Capgemini\Excel_to_Json\venv\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'XX'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "excelToJson.py", line 5, in <module>
    x = df["XX"][0]
  File "C:\Users\aironsid\Documents\Capgemini\Excel_to_Json\venv\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\aironsid\Documents\Capgemini\Excel_to_Json\venv\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'XX'

似乎找不到列名。

我用这个video 作为参考

import pandas

xl = pandas.ExcelFile("file.xlsx")
# df = xl.parse("Text")
# print(df.columns)
# # x = df["XX"][0]
# # print(x)

df = pandas.Dataframe(xl)
print(df.columns)
# if you can see the columns
print(df["XX"])
# if this is success
dictionary = {"XX": list(df["XX"])}

# writing to file
text_file = open("json_files/Output.json", "w")

# text_file.write(json_str)

text_file.close()

【问题讨论】:

  • 你能告诉我们df.columns的样子和数据框的其他细节吗?
  • df.columns 等于 Index(['Unnamed: 0', 'Unnamed: 1', 'Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'], dtype='object')。这是为什么呢?
  • 我猜是因为您在单元格 A1、B1、C1 等中没有值。在您的情况下,您可能需要将 df 的起点转换为不是 A1 而是 B7..
  • 知道怎么做吗?

标签: python json excel pandas dataframe


【解决方案1】:

请试试这个

df = pd.Dataframe(xl)
print(df.columns)
# if you can see the columns 
print(df["XX"])
# if this is success
dictionary = {"XX": list(df["XX"])}

【讨论】:

  • 我正在获取 Traceback(最近一次通话最后一次):文件“excelToJson.py”,第 8 行,在 df = pandas.Dataframe(xl) 文件“C:\Users\aironsid \Documents\Capgemini\Excel_to_Json\venv\lib\site-packages\pandas_init_.py",第 263 行,在 getattr 中引发 AttributeError(f"module 'pandas' has没有属性 '{name}'") AttributeError: 模块 'pandas' 没有属性 'Dataframe'
  • 好的,试试这个pd.read_excel('path', sheet_name="") 然后转换成数据框
  • 同样的问题。 AttributeError: module 'pandas' has no attribute 'Dataframe'
  • 好的,我看到您的 cmets 中的列为 (0,1, 2,3 ) 尝试在编辑代码后打印 df.head,以便您可以看到数据是如何来的
  • 我得到了一堆类似表格的数据,都很有用。这可能是使用它并获取数据的方式。我可以用它来选择具体的数据吗?
【解决方案2】:

如 cmets 中所述,您需要将 A1 的起点转换为 B7。这可以通过pandas.ExcelFile.parse 的“skiprows”参数和index_col 参数来实现:

import pandas
xl = pandas.ExcelFile("path\to\your\file.xlsx")
df = xl.parse("YourSheetName",index_col=1,skiprows=7)

更多文档/参数见pandas docs

【讨论】:

  • 好的,这解决了空值的问题,但我仍然得到同样的错误,关于键。没有“抢C1:D5”解决方案吗?
  • 您需要将第一行更改为列标题以避免该错误
  • @SanthoshReddy 也就是skirows加1。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
相关资源
最近更新 更多