在 excel 文件中获取 Header
get Header in excel file
我的 excel 数据如下所示:
A B C
1 RAM 53.44 576
2 MOHAN 74.34 345
3 KISHAN 76.65 285
如何从电子表格中提取 headers 内容?
查看 Pandas。它比 Xlrd 好很多并且使用的代码更少。
你应该能够得到它的索引,不管它是否是 header。
根据记忆,您必须在创建数据框后使用 df.head()
。
你的问题确实不清楚。但是,您可以使用 pandas 库来读取文件 excel 格式。
import pandas as pd
print(pd.read_excel('file.xlsx').columns)
调用 .columns
列出所有列的 headers
输出示例:
Index(['id', 'email','server','profession'], dtype='object')
编辑:
好的,现在我看到你有非常奇怪的 first to rows 不符合 pandas 数据结构。最简单的解决方案是跳过前两行。然后导入panadas DataFrame
使用skiprows
跳过前n行。在你的情况下 2
import pandas as pd
print(pd.read_excel('file.xlsx',skiprows=2).columns)
#static code if header at 0,0 position
import xlrd
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
# For row 0 and column 0
sheet.cell_value(0, 0)
for i in range(sheet.ncols):
print(sheet.cell_value(0, i))
我的 excel 数据如下所示:
A B C
1 RAM 53.44 576
2 MOHAN 74.34 345
3 KISHAN 76.65 285
如何从电子表格中提取 headers 内容?
查看 Pandas。它比 Xlrd 好很多并且使用的代码更少。
你应该能够得到它的索引,不管它是否是 header。
根据记忆,您必须在创建数据框后使用 df.head()
。
你的问题确实不清楚。但是,您可以使用 pandas 库来读取文件 excel 格式。
import pandas as pd
print(pd.read_excel('file.xlsx').columns)
调用 .columns
列出所有列的 headers
输出示例:
Index(['id', 'email','server','profession'], dtype='object')
编辑:
好的,现在我看到你有非常奇怪的 first to rows 不符合 pandas 数据结构。最简单的解决方案是跳过前两行。然后导入panadas DataFrame
使用skiprows
跳过前n行。在你的情况下 2
import pandas as pd
print(pd.read_excel('file.xlsx',skiprows=2).columns)
#static code if header at 0,0 position
import xlrd
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
# For row 0 and column 0
sheet.cell_value(0, 0)
for i in range(sheet.ncols):
print(sheet.cell_value(0, i))