anurag pandey
anurag pandey

Reputation: 53

how to get a dataframe column horizontal

i have a DataFrame named df,

\   attempts       name qualify  score
a         3     Anurag     yes   12.5
b         3       Dima      no    9.0
c         2  Katherine     yes   16.5
d         3      James      no    NaN
e         2      Emily      no    9.0
f         3    Michael     yes   20.0
g         1    Matthew     yes   14.5
h         1      Laura      no    NaN
i         2      Kevin      no    8.0
j         1      Jonas     yes   19.0

by this 'df.score' I'm getting

a    12.5
b     9.0
c    16.5
d     NaN
e     9.0
f    20.0
g    14.5
h     NaN
i     8.0
j    19.0
Name: score, dtype: float64

i want it Horizontal

a     b    c     d    e    f     g     h    i    j
12.5  9.0  16.5  NaN  9.0  20.0  14.5  NaN  8.0  19.0

Upvotes: 0

Views: 804

Answers (1)

yatu
yatu

Reputation: 88226

You can just index the dataframe and then transpose. Note that you need a 2d shaped pandas object in order to transpose, so after obtaining the pd.Series by indexing on score, you can construct a dataframe with to_frame() or with the constructor itself:

df.score.to_frame().T

        a    b     c   d    e     f     g   h    i     j
score  12.5  9.0  16.5 NaN  9.0  20.0  14.5 NaN  8.0  19.0

Upvotes: 1

Related Questions