![](/img/trans.png)
[英]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.