PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 13, 2022

[FIXED] how to add a value to the end of a pandas column without index

 May 13, 2022     append, indexing, object, pandas, python     No comments   

Issue

i have a Dataframe which i want to add values to end of a column inside it, without using index as i'm doing this inside an object, pandas append didnt work for me for some reason, i tried to add it straight forward and also in another dataframe (x in the code). i wanted something like:

DB=pd.DataFrame(columns=['reviews'])
DB= DB.append(object)# append to reviews column, reviews is also an object 

but when i do it i get and error:

UnboundLocalError: local variable 'DB' referenced before assignment

im expecteing to get something like:

    reviews
0  <__main__.Review object at 0x0000020D2A14BD48>
1  <__main__.Review object at 0x0000020D29F17D88>

my code goes like this:

DB=pd.DataFrame(columns=['reviews'])

class Review:

    def __init__(self, json_string):
       self.json_string=json_string

    def get_text(self):
       json_dict=json.loads(self.json_string)
       return json_dict['body']



class ReviewSearchEngine:

    def __init__(self):
       pass

    def add(self, review:Review):
      
        x = pd.DataFrame(columns=['reviews'])
        x.loc[0, 'reviews'] = review
        ****DB= DB.append(x)****
        return
if __name__ == '__main__':

    search_engine = ReviewSearchEngine()
    file_path = "./review_data.txt"  
    lines = open(file_path).readlines()
    for line in lines:
       review = Review(line) # review is an object
       search_engine.add(review)



 

Solution

I suggested you try this:

class Review:

    def __init__(self, json_string):
        self.json_string = json_string

    def get_text(self):
        json_dict = json.loads(self.json_string)
        return json_dict['body']


class ReviewSearchEngine:

    def __init__(self, db):
        self.db = db

    def add(self, review: Review):
        x = pd.DataFrame(columns=['reviews'])
        x.loc[0, 'reviews'] = review
        self.db = self.db.append(x, ignore_index=True)


if __name__ == '__main__':
    DB = pd.DataFrame(columns=['reviews'])

    search_engine = ReviewSearchEngine(DB)
    file_ = "stack.txt"
    lines = open(file_).readlines()
    for line in lines:
        review = Review(line)  # review is an object
        search_engine.add(review)
    print(search_engine.__dict__['db'])

>>>                                       reviews
0  <__main__.Review object at 0x000001B497563400>
1  <__main__.Review object at 0x000001B4975634F0>
2  <__main__.Review object at 0x000001B49754DEE0>

Please specify argument ignore_index=True when method pd.DataFrame.append to avoid unexpected indexes.



Answered By - Alexandre Mahdhaoui
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing