to_csv未写入更新的DataFrame [英] to_csv is not writing the updated DataFrame

查看:46
本文介绍了to_csv未写入更新的DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在导入一个csv文件作为pandas DataFrame。然后更新该DataFrame,我试图通过覆盖该文件将该信息写回到原始csv文件。代码完成后,我可以看到文件保存时间已更新,因此它似乎已保存了新版本。但是,当我打开文件时,没有任何改变。我已经运行了print语句,以确保DataFrame的值实际上已经更新,并且看起来正确。为什么to_csv不发送更新的DataFrame?

I am importing a csv file as a pandas DataFrame. That DataFrame then gets updated and I am trying to write that information back to the original csv file by overwriting the file. After my code completes, I can see the file save time has updated, so it appears to have saved a new version. However, when I open the file, nothing has changed. I have run print statements to ensure that the DataFrame's values have in fact been updated and they appear to be correct. Why would to_csv not send the updated DataFrame?

self.site_data=pandas.read_csv("site_data.csv",index_col=0, keep_default_na=False)
self.site_data.loc[location,"ADDRESS"] = self.location_details["site_address"]
print(self.site_data.loc[location,"ADDRESS"])
self.site_data.to_csv("site_data.csv", encoding="latin")


推荐答案

所以问题比我想象的要深得多,因此,原因未在我发布的代码中得到体现,但值得庆幸的是,这很容易解决。我的程序中有多个类,其中两个创建了对象

So the problem was much deeper than I had thought and so the cause was not represented in the code I posted, but thankfully it was an easy fix. I have multiple classes in my program and two of them create the object

self.site_data=pandas.read_csv("site_data.csv",index_col=0, keep_default_na=False)

初始化时。这是一个示例,尽管它不是实际的代码:

when initialized. Here's an example, though this is not the actual code:

from example_1 import mass_analysis
from example_2 import mass_import

class mass_analysis:
    def __init__(self):
        self.site_data=pandas.read_csv("site_data.csv",index_col=0, keep_default_na=False)

class mass_import:
    def __init__(self):
        self.site_data=pandas.read_csv("site_data.csv",index_col=0, keep_default_na=False)

class mass_main_ui:
    def __init__(self):
        mass_analysis=mass_analysis()
        mass_import=mass_import()        

因此,在初始化mass_main_ui时,两个类都同时访问文件并在该变量中保存相同的数据。当第一个将数据写回到文件中时,文件/变量的另一个实例仍处于打开状态,因此当该实例写回到文件时,它没有第一个的更改并覆盖了所有内容它做到了。解决方法是删除:

So, when mass_main_ui is initialized, both classes accessed the file at the same time and hold the same data within that variable. When the first one writes the data back to the file, the other instance of the file/variable is still "open" so when THAT one writes back to the file, it doesn't have the changes from the first one and writes over anything that it did. The fix was to remove the:

mass_analysis=mass_analysis()

mass_import=mass_import()

从初始化开始,而是在需要时在程序的其他函数中调用该类,例如:

from the initialization and instead just call that class when needed within the other functions in my program, such as:

mass_import().get_new_data(source_file)

def get_new_data(source_file):
    print("This works")

这篇关于to_csv未写入更新的DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆