1

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)
hzgfx
  • 13
  • 2

1 Answers1

2

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.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • 1
    This is clearly suboptimal. You could just use `pd.read_excel(..., skiprows=1)` and it would do the same thing. – cs95 Apr 22 '18 at 20:53
  • @cᴏʟᴅsᴘᴇᴇᴅ Yeah, you're right (thanks). I read the part about the unnamed columns and stuff, and didn't continue on to the csv part. – Ami Tavory Apr 22 '18 at 20:54
  • 1
    Sure. Also, I've been seeing you more often answering such questions here, so I'd like to give you a word of advice. Please don't answer duplicate questions. You have a gold badge, so please hammer them instead. Rule of thumb: If it can be answered with a single function, it is a duplicate. There are a lot of other common idioms and patterns that have been asked as well. – cs95 Apr 22 '18 at 20:56
  • @cᴏʟᴅsᴘᴇᴇᴅ Yeah, sure. In fact, it's been some time since I returned to being active in SO, and I think I'll take a break again. All the best. – Ami Tavory Apr 22 '18 at 20:57
  • @AmiTavory Hmm, sorry if it came off as brazen. This is generally my advice to any user who I believe is a valuable contributor to the community. The general consensus on meta is that duplicates are not to be answered, and I'm just trying to make sure people are aware of that :) Please don't look at my last comment as a cue to leave, that wasn't my intention. I hope to see you around in future, with more valuable contributions. I believe your answers are genuinely good, and wasted on basic questions that are better off being closed. Peace, and good luck to you too. – cs95 Apr 23 '18 at 18:20
  • @cᴏʟᴅsᴘᴇᴇᴅ Hey, thanks for the message, but really don't worry about it. Everything is fine. I wish you all the best. – Ami Tavory Apr 23 '18 at 20:37