python - 如何在没有索引的熊猫列的末尾添加一个值
问题描述
我有一个数据框,我想在其中的列末尾添加值,而不使用索引,因为我在对象内执行此操作,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)
解决方案
我建议你试试这个:
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
以避免意外索引。
推荐阅读
- python - 从 Jupyter 笔记本中删除空目录错误
- c++14 - Merge Sort implementation outputs wrong results
- javascript - 在页面重新加载之前将错误消息保留更长时间
- xcodebuild - 使用 `xcodebuild` 为 Mac 的 Catalyst/UIKit 进行命令行构建?
- javascript - 打印出更新的数组值并将其替换为表格单元格
- ascii - gnuplot:哑终端中多个图的叠加顺序
- python - 如何搜索字符串,拉出一段,并将其用作变量
- javascript - 表单提交成功的显示模式
- svn - 具有历史记录的存储库之间的 SVN 部分复制
- java - ClassNotFoundException com.mysql.jdbc.driver jar 来自导出的 jar 文件