Need help to solve the Unnamed and to change it in dataframe in pandas

喜你入骨 提交于 2019-12-11 08:54:39

问题


how set my indexes from "Unnamed" to the first line of my dataframe in python

import pandas as pd

df = pd.read_excel('example.xls','Day_Report',index_col=None ,skip_footer=31 ,index=False)

df = df.dropna(how='all',axis=1)
df = df.dropna(how='all')
df = df.drop(2)

回答1:


To set the column names (assuming that's what you mean by "indexes") to the first row, you can use

df.columns = df.loc[0, :].values

Following that, if you want to drop the first row, you can use

df.drop(0, inplace=True)

Edit

As coldspeed correctly notes below, if the source of this is reading a CSV, then adding the skiprows=1 parameter is much better.



来源:https://stackoverflow.com/questions/49970526/need-help-to-solve-the-unnamed-and-to-change-it-in-dataframe-in-pandas

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