Home > other >  Pandas: transpose doesn't transpose rows to columns
Pandas: transpose doesn't transpose rows to columns

Time:10-22

I am reading in a csv with the following code:

df=pd.read_csv(file, index_col="Votes")

Which gives this result:

votes  10  20  5  30
  1st   a   e  b   c
  2nd   b   a  c   e

I want to transpose this to look like the following:

votes  1st  2nd 
   10    a    b
   20    e    a
    5    b    c
   30    c    e

And I want votes, 1st, 2nd to be columns. But transposing like this df_t = df.T keeps the columns as 10, 20 etc.

How do I get the columns to be recognized as votes, 1st etc?

If it helps, my proximate goal is to sum votes for each a in 1st, etc. (ranked choice voting).

CodePudding user response:

From your DataFrame :

>>> import pandas as pd
>>> from io import StringIO

>>> df = pd.read_csv(StringIO("""
votes,10,20,5,30
1st,a,e,b,c
2nd,b,a,c,e
"""), sep=',', index_col="votes")
>>> df
       10   20  5   30
votes               
1st    a    e   b   c
2nd    b    a   c   e

We can indeed use T to transpose it as expected :

>>> df_T = df.T
>>> df_T
votes   1st 2nd
10      a   b
20      e   a
5       b   c
30      c   e

Then, as asked in the commentary section, we can setup the index like so :

>>> df_T['votes'] = df_T.index
>>> df_T = df_T.reset_index(drop=True)
>>> df_T.columns.name = None
>>> df_T
    1st 2nd votes
0   a   b   10
1   e   a   20
2   b   c   5
3   c   e   30
  • Related