Reseting Index in dataframe on Axis 0
Question:
So I am trying to transpose a data that is coming from excel. Once I transpose it the Index numbers are located above the columns..so when I try to reset the index I get something like this:
The problem is I need column names which I will use later in my project and then I need the data which will match those column names. But it doesnt work since the columns are now indexed numbers.
And what I want is this:
I tried everything but nothing seems to work, I tried resetting the index at level 0, setting the new index but it always gets messed up. Any help or suggestion on what to do is much appreciated!
Answers:
You can try doing this
df = df.T
df.reset_index(inplace=True)
df.columns = df.iloc[0] # this will set the zeroth index as columns
df = df[1:] # this will remove the zeroth index from the dataframe
df.reset_index(inplace=True)
df.drop('index', axis=1, inplace=True)
df.index.name = None
So I am trying to transpose a data that is coming from excel. Once I transpose it the Index numbers are located above the columns..so when I try to reset the index I get something like this:
The problem is I need column names which I will use later in my project and then I need the data which will match those column names. But it doesnt work since the columns are now indexed numbers.
And what I want is this:
I tried everything but nothing seems to work, I tried resetting the index at level 0, setting the new index but it always gets messed up. Any help or suggestion on what to do is much appreciated!
You can try doing this
df = df.T
df.reset_index(inplace=True)
df.columns = df.iloc[0] # this will set the zeroth index as columns
df = df[1:] # this will remove the zeroth index from the dataframe
df.reset_index(inplace=True)
df.drop('index', axis=1, inplace=True)
df.index.name = None