python - Pandas df.to_csv() 保存我的文件的旧版本,而不是我修改过的文件
问题描述
我有一个数据框,我正在替换 NaN 和零值。在 jupyter notebook 中看起来一切都很好,但是当我使用 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
方法时定义这些值。
对于逗号分隔值:
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)
推荐阅读
- python - 如何删除列表中的字典元素?
- biopython - 我需要安装 MUSCLE 和 ClustalW 才能使用 biopython 工具吗?
- java - 单击预测按钮时,应用程序崩溃
- python - Pandas Dataframe:如何获取整个数据帧的真实部分?
- twitter-bootstrap - 尝试在 Bootstrap 中使用 Toggler 但它不起作用
- java - Spring AbstractAuthenticationFailureEvent 不起作用。身份验证失败时
- robotframework - 有没有其他方法可以用第一个答案来解决这个问题?
- python - 覆盖多选 BooleanField 请求响应 django
- openstack - 如何在 openstack trove 中创建 percona 实例或 Percona XtraDB Cluster?
- oracle - CREATE ANY TABLE 不足以创建任何表?