Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the extra row (or column) after transpose() in Pandas

After using transpose on a dataframe there is always an extra row as a remainder from the initial dataframe's index for example:

import pandas as pd

df = pd.DataFrame({'fruit':['apple','banana'],'number':[3,5]})
df
    fruit  number
0   apple       3
1  banana       5
df.transpose()
        0       1
fruit   apple  banana
number      3       5

Even when i have no index:

df.reset_index(drop = True, inplace = True)
df
    fruit  number
0   apple       3
1  banana       5

df.transpose()
        0       1
fruit   apple  banana
number      3       5

The problem is that when I save the dataframe to a csv file by:

df.to_csv(f)

this extra row stays at the top and I have to remove it manually every time.

Also this doesn't work:

 df.to_csv(f, index = None)

because the old index is no longer considered an index (just another row...).

It also happened when I transposed the other way around and I got an extra column which i could not remove.

Any tips?

like image 336
Helena K Avatar asked Jul 01 '16 15:07

Helena K


People also ask

How do I get rid of extra index in Pandas?

The most straightforward way to drop a Pandas dataframe index is to use the Pandas . reset_index() method. By default, the method will only reset the index, forcing values from 0 - len(df)-1 as the index. The method will also simply insert the dataframe index into a column in the dataframe.

How do I transpose columns to rows in Pandas?

Pandas DataFrame: transpose() functionThe transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. If True, the underlying data is copied. Otherwise (default), no copy is made if possible.


2 Answers

I had the same problem, I solved it by reseting index before doing the transpose. I mean df.set_index('fruit').transpose():

import pandas as pd

df = pd.DataFrame({'fruit':['apple','banana'],'number':[3,5]})
df
    fruit   number
0   apple   3
1   banana  5

And df.set_index('fruit').transpose() gives:

fruit   apple   banana
number  3       5
like image 194
user1742571 Avatar answered Oct 15 '22 23:10

user1742571


Instead of removing the extra index, why don't try setting the new index that you want and then use slicing ?

step 1: Set the new index you want:
df.columns = df.iloc[0]
step 2: Create a new dataframe removing extra row.
df_new = df[1:]

like image 44
Radhika Nair Avatar answered Oct 15 '22 22:10

Radhika Nair