简体   繁体   中英

Pandas df.to_csv() saves the old version of my file instead of the one i have modified

I have a dataframe i'm replacing the NaN and zero values on. It all looks good when it's in jupyter notebook but when i use df.to_csv() it creates basically a copy of the original dataframe with all the zeros and NaN values.

I have tried every combination and way to write the path to where it should go.

df = pd.read_csv(r"C:\Users\Eddie\Downloads\pandas\Deformation.txt", error_bad_lines=False)
df.dropna(axis=1, how="all", inplace=True)

suffixes = ["_A", "_B"]

for suffix in suffixes:
    # Välj ut alla DIG*_*-kolumner och spara i en lista
    dig_cols = [col for col in df.columns if (col.replace(" 
    ","").startswith("DI") and col.endswith(suffix))]

    # Säkerställ att alla DIG*_*-kolumner är decimaltal
    for col in dig_cols:
        df[col] = df[col].astype(float)
        df[col].replace(0, np.nan, inplace=True)
        df[col].fillna(method="ffill", inplace=True)

path = r"C:\Users\Eddie\Downloads\pandas"
df.to_csv(os.path.join(path, "Deformation_new.txt"))

You Should try with the absolute path as follows, However when you use to_csv it means comma separated Values ,So, you may either want to export values by comma separated or tab separated which you can define While working with DataFrame.to_csv method.

For comma Separated Values:

df.to_csv(r"C:\Users\Eddie\Downloads\pandas\Deformation_new.txt", sep=",", index=False)

For tab Separated Values:

df.to_csv(r"C:\Users\Eddie\Downloads\pandas\Deformation_new.txt", sep="\t", index=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM