How do I use my first row in my spreadsheet for my Dataframe column names instead of 0 1 2…etc?

二次信任 提交于 2021-02-10 12:12:41

问题


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?

I tried using pandas and openpyxl modules to turn my Excel spreadsheet into a dataframe.

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.


回答1:


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)



回答2:


you can reset the columns to be the first row of your dataframe:

df.columns = df.iloc[0, :]
df.drop(df.index[0], inplace=True)
df



来源:https://stackoverflow.com/questions/56981186/how-do-i-use-my-first-row-in-my-spreadsheet-for-my-dataframe-column-names-instea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!