2
votes

I have a df which looks like this:

                Column 1
Channel          
Apples            1.0
Oranges           2.0
Puppies           3.0
Ducks             4.0

I would like to rename the axis so it looks like this:

Channel         Column 1          
Apples            1.0
Oranges           2.0
Puppies           3.0
Ducks             4.0

I tried these but got the same error msg:

merged_df.rename_axis("Channel")
merged_df.rename_axis("Channel", axis='columns')

TypeError: 'str' object is not callable
1
Isn't this just a display issue? when your index has a name, in your case 'Channel' then a row gap is inserted so you can tell the difference between the index and the columns - EdChum
That's it, I don't want there to be a row gap. I'm trying to recreate the data in an excel file and it doesnt have a row gap. - Boosted_d16
Well I'm not sure you can do that because I can't find an option to change the display option, if you think about it, it behaves this way to help you I don't see what the harm is - EdChum
@Boosted_d16 Isn't reset_index() pretty close to what you want? It seems gap-less and Excel-friendly. - Ami Tavory
Yes but then I have the index ints, id have to manually delete that column. Problem is, I'm trying to replicate something hence I need to do this odd task. - Boosted_d16

1 Answers

2
votes

This is kind of a hack to get you what you eventually wanted it to look like.

df = pd.read_csv(data, sep='\s{2,}', index_col='Channel', engine='python')
df

Image

df_excel_format = df.rename_axis('Channel', axis=1)
del(df_excel_format.index.name)
df_excel_format

Image