[英]How to save pandas dataframe to a .csv with specific filename?
For example I import csv file as data frame and work on some process on pandas 例如,我将csv文件作为数据框导入并在熊猫上进行某些处理
df = pd.read_csv('./label/101_5603_2019-05-02~2019-05-18.csv')
rest of codes to work on the data
How do I save the result to a new csv file with the names including 8 first character of the original csv files (101_5603.csv)? 如何将结果保存到名称包含原始csv文件(101_5603.csv)的8个首字符的新csv文件中? Thanks!
谢谢!
Use: 采用:
import os
f = './label/101_5603_2019-05-02~2019-05-18.csv'
url, ext = os.path.splitext(os.path.basename(f))
new = url[:8] + ext
print (new)
101_5603.csv
new = os.path.dirname(f) + '/' + url[:8] + ext
print (new)
./label/101_5603.csv
new = os.path.join(os.path.dirname(f), url[:8] + ext)
print (new)
./label\101_5603.csv
And pass it to DataFrame.to_csv
: 并将其传递给
DataFrame.to_csv
:
df.to_csv(new, index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.