Skip to content Skip to sidebar Skip to footer

To_csv Is Not Writing The Updated 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. A

Solution 1:

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()        

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()

and

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")

Post a Comment for "To_csv Is Not Writing The Updated DataFrame"