Python Forum
Using .append() with list vs dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using .append() with list vs dataframe
#1
My program adds rows to a dataframe by using .append() with a dictionary. In order to make this work, I think I had to do df = df.append() rather than just df.append(). Going back and retrying this afternoon, though, I think I've seen it work either way?

Here, though:

colors = []
for i in summary_results['ROI%']:
    if i < 0:
        colors = colors.append('r')
    else:
        colors = colors.append('g')
print(colors)
I get AttributeError: 'NoneType' object has no attribute 'append' . I need just colors.append('r') rather than assignment.

What's going on with when and why .append() may or may not require assignment (for lists vs. dataframes) in order to work?
Reply
#2
Quote:DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=False)[source]¶
Append rows of other to the end of caller, returning a new object.

Quote:list.append(x)
Add an item to the end of the list. Equivalent to a[len(a):] = [x].

Notice that DataFrame.append says it returns a new object. Python.list.append says nothing about a return value, and it returns None.

The word is the same. Use is similar. Results are completely different.
Mark17 likes this post
Reply
#3
(Jun-10-2022, 09:03 PM)deanhystad Wrote:
Quote:DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=False)[source]¶
Append rows of other to the end of caller, returning a new object.

Quote:list.append(x)
Add an item to the end of the list. Equivalent to a[len(a):] = [x].

Notice that DataFrame.append says it returns a new object. Python.list.append says nothing about a return value, and it returns None.

The word is the same. Use is similar. Results are completely different.

With regard to list.append, does the AttributeError then mean you can't assign that to a variable because there's nothing to assign since nothing is returned?

And with regard to DataFrame.append, if I don't assign the new object to a variable then what happens to it? This...
import pandas as pd
a = [['blue', 3], ['red', 8], ['green', 0]]
df = pd.DataFrame(a)
print(df)
print()
df.append([['grey',12]])
print(df)
...prints out the same thing twice. If I do df = df.append([['grey',12]]) then I see the added row.
Reply
#4
append is deprecated

import pandas as pd
df = pd.DataFrame([['blue', 3], ['red', 8], ['green', 0]])
print(df)

df2 = pd.DataFrame([['grey',12]])
df = pd.concat([df, df2], ignore_index=True)
print(df)
Output:
0 1 0 blue 3 1 red 8 2 green 0 0 1 0 blue 3 1 red 8 2 green 0 3 grey 12
Reply
#5
import pandas as pd

rg = pd.DataFrame([['red', 0], ['green', 1]])
rgb = rg.append([['blue', 2]])

print(rg, "\n")
print(rgb)
Output:
0 1 0 red 0 1 green 1 0 1 0 red 0 1 green 1 0 blue 2
Notice that the dataframe referenced by "rg" is unchanged. It only contains "red" and "green". "rgb" references a new dataframe object that was created by the append() function. This new dataframe contains the original and the new row "blue".

Python lists are different. When you append to a list, the original list is modified to hold the new values.
rg = ["red", "green"]
rgb = rg.append("blue")
print(rg)
print(rgb)
Output:
['red', 'green', 'blue'] None
Notice that the list object referenced by "rg" contains the color "blue". The original list object was modified to contain the appended value. Also notice that "rgb" refrences None. It is common for Python functions to return None when they don't create any objects.

Python objects live only as long as they are referenced. Unreferenced objects get garbage collected and their memory is reused to make new objects. color = "blue" assigns a str object to a variable named "color". Right now there is only one reference to the str object "blue". If I do this, color = 1, the variable color now references an int object. Now the str object "blue" is not referenced by any variables. It's reference count is zero. There is no way for me to use the str object "blue" anymore because I don't have any variables that reference it. In many languages "blue" would continue to exist in limbo, taking up space until the program ends. In Python, assigning color = 1 not only assigns color to reference an int object, it unassigns color to stop referencing the "blue" object. The "blue" object reference count drops to zero, and Python puts it in the garbage collector.

In your example where you do this: df = df.append([['grey',12]]), the variable "df" is reassigned to reference the new dataframe object created by the append() function. The original dataframe object is no longer referenced by any variables, so it gets garbage collected.

Is that clear?
Mark17 likes this post
Reply
#6
using append I always get a message

Output:
FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
Mark17 likes this post
Reply
#7
(Jun-11-2022, 10:07 PM)deanhystad Wrote: Python objects live only as long as they are referenced. Unreferenced objects get garbage collected and their memory is reused to make new objects. color = "blue" assigns a str object to a variable named "color". Right now there is only one reference to the str object "blue". If I do this, color = 1, the variable color now references an int object. Now the str object "blue" is not referenced by any variables. It's reference count is zero. There is no way for me to use the str object "blue" anymore because I don't have any variables that reference it. In many languages "blue" would continue to exist in limbo, taking up space until the program ends. In Python, assigning color = 1 not only assigns color to reference an int object, it unassigns color to stop referencing the "blue" object. The "blue" object reference count drops to zero, and Python puts it in the garbage collector.

In your example where you do this: df = df.append([['grey',12]]), the variable "df" is reassigned to reference the new dataframe object created by the append() function. The original dataframe object is no longer referenced by any variables, so it gets garbage collected.

Is that clear?

That's good stuff. So the answer to my question about where the new dataframe object goes if not assigned is that it disappears into the recycle bin. Thanks!
Reply
#8
(Jun-12-2022, 11:00 AM)Axel_Erfurt Wrote: using append I always get a message

Output:
FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

I saw this but upon closer look it seemed like it was pertaining to appending one dataframe to another. I am appending a dictionary to a dataframe (to add a new row), which is why I thought the deprecation did not apply. I could certainly be wrong.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 480 Mar-14-2024, 06:26 PM
Last Post: flash77
Question How to append integers from file to list? Milan 8 1,451 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  function returns dataframe as list harum 2 1,404 Aug-13-2022, 08:27 PM
Last Post: rob101
  read a text file, find all integers, append to list oldtrafford 12 3,563 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  List of dataframe values beginning with x,y or z glidecode 3 1,937 Nov-08-2021, 10:16 PM
Last Post: glidecode
  Reading data to python: turn into list or dataframe hhchenfx 2 5,384 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,330 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  Add a row to a dataframe or append whole dataframe. tsurubaso 1 1,461 Jan-07-2021, 01:53 AM
Last Post: tsurubaso
  convert list to five columns dataframe in sequence tonycat 2 2,481 Sep-29-2020, 06:47 AM
Last Post: tonycat
  How to append to list a function output? rama27 5 6,728 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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