首页 > 解决方案 > 如何在没有索引的熊猫列的末尾添加一个值

问题描述

我有一个数据框,我想在其中的列末尾添加值,而不使用索引,因为我在对象内执行此操作,pandas append 由于某种原因对我不起作用,我尝试直接添加它并且在另一个数据框中(代码中的 x )。我想要类似的东西:

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

但是当我这样做时,我得到了错误:

UnboundLocalError: local variable 'DB' referenced before assignment

我期待得到类似的东西:

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

我的代码是这样的:

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)



 

标签: pythonpandasobjectindexingappend

解决方案


我建议你试试这个:

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>

ignore_index=True请在方法时指定参数pd.DataFrame.append以避免意外索引。


推荐阅读