简体   繁体   中英

Issue with saving CSV file how can I fix this in python pandas?

I'm having trouble dropping columns and saving the new data frame as a CSV file.

Code:

import pandas as pd 
file_path = 'Downloads/editor_events.csv'
df = pd.read_csv(file_path, index_col = False, nrows= 1000)
df.to_csv(file_path, index = False)
df.to_csv(file_path)

The code executes and doesn't give any error. I've looked in my root directory but can't see any new csv file

First: you are saving the same file that you are reading, so you won't see any new csv files. All you are doing right now is rewriting the same file.

But since I can guess you just show it as simple example of what you want to do, I will move to second: Make sure that your path is correct. Try to write the full path, like 'c:\\Users\\AwesomeUser\\Downloads\\editor_events.csv' instead of just 'Downloads\\editor_events.csv' .

Check file in folder in which you are running python script. And you are saving with same name, so you can check modified time to confirm it. Also you are not dropping columns as per posted code, you are just taking 1000 rows and saving it.

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