agent18
agent18

Reputation: 2297

add row to dataframe pandas

I want to add a median row to the top. Based on this stack answer I do the following:

pd.concat([df.median(),df],axis=0, ignore_index=True)

Shape of DF: 50000 x 226
Shape expected: 50001 x 226
Shape of modified DF: 500213 x 227 ???

What am I doing wrong? I am unable to understand what is going on?

Upvotes: 0

Views: 146

Answers (2)

Alan Jose Tom
Alan Jose Tom

Reputation: 131

pd.concat([df.median(),df],axis=0, ignore_index=True)

this code creates a row for you but that is not a DataFrame it is a Series. So you want to convert the series to DataFrame so you can use

.to_frame().T

to your code then your code become

pd.concat([df.median().to_frame().T,df],axis=0, ignore_index=True)

Upvotes: 1

Ferris
Ferris

Reputation: 5601

Maybe what you wanted is like this:

dfn = pd.concat([df.median().to_frame().T, df], ignore_index=True)

create some sample data:

df = pd.DataFrame(np.arange(20).reshape(4,5), columns= list('ABCDE'))
dfn = pd.concat([df.median().to_frame().T, df])

df
    A   B   C   D   E
0   0   1   2   3   4
1   5   6   7   8   9
2   10  11  12  13  14
3   15  16  17  18  19


df.median().to_frame().T
     A    B    C     D     E
0  7.5  8.5  9.5  10.5  11.5


dfn
      A     B     C     D     E
0   7.5   8.5   9.5  10.5  11.5
0   0.0   1.0   2.0   3.0   4.0
1   5.0   6.0   7.0   8.0   9.0
2  10.0  11.0  12.0  13.0  14.0
3  15.0  16.0  17.0  18.0  19.0

df.median() is an Series, with row index of A, B, C, D, E, so when you concat df.median() with df, the result is that:

pd.concat([df.median(),df], axis=0)

      0     A     B     C     D     E
A   7.5   NaN   NaN   NaN   NaN   NaN
B   8.5   NaN   NaN   NaN   NaN   NaN
C   9.5   NaN   NaN   NaN   NaN   NaN
D  10.5   NaN   NaN   NaN   NaN   NaN
E  11.5   NaN   NaN   NaN   NaN   NaN
0   NaN   0.0   1.0   2.0   3.0   4.0
1   NaN   5.0   6.0   7.0   8.0   9.0
2   NaN  10.0  11.0  12.0  13.0  14.0
3   NaN  15.0  16.0  17.0  18.0  19.0

Upvotes: 2

Related Questions