Home > front end >  How to replicate DataFrame.append of pandas.Series by using pandas.concat?
How to replicate DataFrame.append of pandas.Series by using pandas.concat?

Time:06-03

I have a code in which I create a pivot table, I apply a function to each column, and I append the result as a row to the dataframe. The code behaves like this:

>>> import pandas as pd
>>> df = pd.DataFrame({
... 'id':['A','B','C','A','B','C'],
... 'par':['x','x','x','y','y','y'],
... 'val':[1,2,3,4,5,6]})
>>> pv = df.pivot_table(index="id", columns='par', values="val", aggfunc="sum")
>>> avg = pv.mean().rename('MyFunc')
>>> pv.append(avg)
par       x    y
id
A       1.0  4.0
B       2.0  5.0
C       3.0  6.0
MyFunc  2.0  5.0

Which is the output I expect. However, documentation of pandas.DataFrame.append says:

Deprecated since version 1.4.0: Use concat() instead. For further details see Deprecated DataFrame.append and Series.append

When I try to replicate this with pandas.concat I get a different output

>>> pd.concat([pv, avg])
     x    y    0
A  1.0  4.0  NaN
B  2.0  5.0  NaN
C  3.0  6.0  NaN
x  NaN  NaN  2.0
y  NaN  NaN  5.0
>>> pd.concat([pv, avg], axis=1)
     x    y  MyFunc
A  1.0  4.0     NaN
B  2.0  5.0     NaN
C  3.0  6.0     NaN
x  NaN  NaN     2.0
y  NaN  NaN     5.0

Is it possible to achieve the same result of append by using concat?

CodePudding user response:

If append Series is possible use DataFrame.loc:

pv.loc['MyFunc'] = pv.mean()
print (pv)
par       x    y
id              
A       1.0  4.0
B       2.0  5.0
C       3.0  6.0
MyFunc  2.0  5.0

Or concat with convert Series to one row DataFrame by Series.to_frame and transpose:

print (avg.to_frame().T)
par       x    y
MyFunc  2.0  5.0

df = pd.concat([pv, avg.to_frame().T])
print (df)
par       x    y
A       1.0  4.0
B       2.0  5.0
C       3.0  6.0
MyFunc  2.0  5.0
  • Related