[英]Pandas df.to_csv() saves dict values as string. How can I get the dicts back when calling pd.read_csv()?
[英]Pandas df.to_csv() saves the old version of my file instead of the one i have modified
我有一個數據框,我要替換NaN和零值。 當它在jupyter筆記本中時,一切看起來都很好,但是當我使用df.to_csv()時,它基本上會創建具有所有零和NaN值的原始數據幀的副本。
我已經嘗試了每種組合和方式來寫出應該到達的路徑。
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"))
您應該嘗試按如下所示使用絕對路徑,但是,當使用to_csv
它表示用comma
分隔的值,因此,您可能要用comma
分隔或tab
分隔的方式導出值,可以在使用DataFrame.to_csv
方法時定義DataFrame.to_csv
。
對於逗號分隔的值:
df.to_csv(r"C:\Users\Eddie\Downloads\pandas\Deformation_new.txt", sep=",", index=False)
對於制表符“分隔值”:
df.to_csv(r"C:\Users\Eddie\Downloads\pandas\Deformation_new.txt", sep="\t", index=False)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.