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

6
  • 2
    It is not an inplace operation. Try noTEU = noTEU.append(imo_vessel) Commented Sep 16, 2016 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? Commented Sep 16, 2016 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 Commented Sep 16, 2016 at 16:00
  • There's a similar one asked over here: stackoverflow.com/questions/16597265/… Commented Sep 16, 2016 at 16:02
  • 2
    @ayhan: Totally agree with that. Even the docs don't indicate whether append operation takes place inplace or not. Commented Sep 16, 2016 at 16:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.