Python Forum
[Solved:] How to use pd.append() ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [Solved:] How to use pd.append() ? (/thread-36515.html)



[Solved:] How to use pd.append() ? - ju21878436312 - Feb-28-2022

Can someone tell me, why the first example of how to use pd.append() works, and the second not?

# 1) This works fine: 

df1 = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
df1.append(df2)
df1

# 2) However this does not work:

d3 = {'1':[100],'pos':["middle"]}
d4 = {'1':[200],'pos':["middle"]}

df3 = pd.DataFrame(data=d3)
df4 = pd.DataFrame(data=d4)

df3.append(df4)
df3
I am using the 2nd way to create my dataframe, and prefer to combine my dataframes with

pd.append() 
instead of
pd.concat()



RE: How to use pd.append() ? - snippsat - Feb-28-2022

If save it in a variable it should work the same.
import pandas as pd

df1 = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
df_new1 = df1.append(df2)
print(df_new1)

d3 = {'1':[100],'pos':["middle"]}
d4 = {'1':[200],'pos':["middle"]}
df3 = pd.DataFrame(data=d3)
df4 = pd.DataFrame(data=d4)
df_new2 = df3.append(df4)
print(df_new2)
Output:
A B 0 1 2 1 3 4 0 5 6 1 7 8 1 pos 0 100 middle 0 200 middle



RE: How to use pd.append() ? - deanhystad - Feb-28-2022

This doesn't work either:
# 1) This works fine:   Oh no it doesn't!
 
df1 = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
df1.append(df2)
print(df1)
Output:
A B 0 1 2 1 3 4
In both cases .append() creates a new dataframe instead of modifying the dataframe in place.


RE: How to use pd.append() ? - ju21878436312 - Mar-01-2022

thank you!