問題描述
如何在我的計算機上保存我用 Python 編輯的 CSV 文件? (How do I save a CSV file on my computer which i have edited in Python?)
我使用 python 刪除了 CSV 文件中的所有 NaN 值,但我想將新文件保存在我的計算機上,以便我可以通過 excel 打開它。
data = pd.read_csv('PAAD1.csv')
new_data = data.dropna(axis = 0, how ='any')
print("Old data frame length:", len(data), "\nNew data frame length:",
len(new_data), "\nNumber of rows with at least 1 NA value: ",
(len(data)‑len(new_data)))
參考解法
方法 1:
You can save it to your current working directory using
new_data.to_csv('PAAD1_noNA.csv')
If you want it saved in another directory, then use
import pandas as pd
path = 'path/to/current/working/directory/'
new_data.to_csv(path + 'PAAD1_noNA.csv')
(by Vihaan Misra、David Collins)