0

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?

agent18
  • 2,109
  • 4
  • 20
  • 34
  • 3
    Can you give us a sample dataframe with more entries and expected output. May use this link as guide to give a clearer question - https://stackoverflow.com/help/minimal-reproducible-example – Vaebhav Dec 29 '20 at 10:37

2 Answers2

2

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
Ferris
  • 5,325
  • 1
  • 14
  • 23
  • Thanks :) I tried `pd.DataFrame(df.median())` but that didn't work as well. I guess because of the missing transpose! – agent18 Dec 29 '20 at 12:25
1
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)
Alan Jose Tom
  • 131
  • 2
  • 5