[英]How do I use my first row in my spreadsheet for my Dataframe column names instead of 0 1 2…etc?
I want my dataframe to display the first row names as my dataframe column name instead of numbering from 0 etc. How do I do this? 我希望我的数据框显示第一行名称作为数据框的列名称,而不是从0等开始编号。我该如何做?
I tried using pandas and openpyxl modules to turn my Excel spreadsheet into a dataframe. 我尝试使用pandas和openpyxl模块将Excel电子表格转换为数据框。
import pandas as pd
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
wb = load_workbook(filename='Budget1.xlsx')
print(wb.sheetnames)
sheet_ranges=wb['May 2019']
print(sheet_ranges['A3'].value)
ws=wb['May 2019']
df=pd.DataFrame(ws.values)
print(df) # This displays my dataframe.
I expect my column titles of my dataframe to display Date, Description, and Amount instead of 0, 1, 2. 我希望数据框的列标题显示日期,描述和金额,而不是0、1、2。
After reading data dataframe using pandas you can separate first row then use that as column name: 使用熊猫读取数据框后,您可以分隔第一行,然后将其用作列名:
columnNames = df.iloc[0]
df = df[1:]
df.columns = columnNames
Or, you can directly read using pandas that will set first row as column name: 或者,您可以使用将第一行设置为列名的熊猫直接阅读:
excelDF = pd.ExcelFile('Budget1.xlsx')
df1 = pd.read_excel(excelDF, 'SheetNameThatYouWantTORead')
print(df1.columns)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.