1

I am working one week with python and I need some help. I want that if certain condition is fulfilled, it adds a value to a database. My program doesn't give an error but it doesn't append an element to my database

import pandas as pd
noTEU = pd.DataFrame() # empty database
index_TEU = 0
for vessel in list:
    if condition is fullfilled:
        imo_vessel = pd.DataFrame({'imo': vessel}, index=[index_TEU])   
        noTEU.append(imo_vessel) # I want here to add an element to my database
        index_TEU = index_TEU + 1

If I run this, at the end I still get an empty dataframe. I have no idea why it doesn't do what I want it to do

Koen
  • 346
  • 1
  • 3
  • 14
  • 2
    It is not an inplace operation. Try `noTEU = noTEU.append(imo_vessel)` – ayhan Sep 16 '16 at 15:58
  • 1
    Do we have a canonical question for this? Not specific to append but in general, for methods returning a new dataframe? – ayhan Sep 16 '16 at 15:59
  • 2
    @ayhan there are a ton of questions with the same answer but there isn't a canonical one, it should be created as it gets tiresome explaining this – EdChum Sep 16 '16 at 16:00
  • There's a similar one asked over here: http://stackoverflow.com/questions/16597265/appending-to-an-empty-data-frame-in-pandas – Nickil Maveli Sep 16 '16 at 16:02
  • @NickilMaveli yeah that might be a good target for this question but I don't know if we could use it if the question was about, say, drop instead of append. It might be better if someone posted a general question about this. – ayhan Sep 16 '16 at 16:07
  • 2
    @ayhan: Totally agree with that. Even the docs don't indicate whether `append` operation takes place `inplace` or not. – Nickil Maveli Sep 16 '16 at 16:12

1 Answers1

1

You should reassign the dataframe such as:

import pandas as pd
noTEU = pd.DataFrame() # empty database
index_TEU = 0
for vessel in list:
    if condition is fullfilled:
        imo_vessel = pd.DataFrame({'imo': vessel}, index=[index_TEU])   
        noTEU = noTEU.append(imo_vessel) # I want here to add an element to my database
        index_TEU = index_TEU + 1

and don't use the keyword list for a List because it's included in the Python syntax.

Fabio Lamanna
  • 20,504
  • 24
  • 90
  • 122