Python Forum
[Solved:] How to use pd.append() ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved:] How to use pd.append() ?
#1
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()
Reply
#2
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
ju21878436312 likes this post
Reply
#3
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.
ju21878436312 likes this post
Reply
#4
thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [Beautifulsoup] Find if element exists, and edit/append? Winfried 2 4,316 Sep-03-2022, 10:14 PM
Last Post: Winfried
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,610 Apr-22-2020, 01:01 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020