简体   繁体   English

如何将熊猫数据框保存到具有特定文件名的.csv文件?

[英]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.

相关问题 如何将背景颜色添加到熊猫数据框的特定列并将该彩色数据框保存到同一个 csv 中? - How to to add Background color to a specific column of pandas dataframe and save that colored dataframe into that same csv? Pandas:如何从 dataframe 的特定列中获取每个类别的样本行并保存在单个 csv 中? - Pandas: How to get the sample rows from each category from specific column in dataframe and save in single csv? 无法将熊猫数据框保存到 csv - Cannot save pandas dataframe to csv 使用标头和特定文件名将spark数据帧导出到.csv - Exporting spark dataframe to .csv with header and specific filename Python:如何为熊猫数据框中的每个 ID 保存不同的 .csv? - Python: how to save different .csv for each ID in a pandas dataframe? 如何在覆盖模式下将 pandas dataframe 保存到 csv? - How can i save a pandas dataframe to csv in overwrite mode? 如何将动态生成的熊猫数据框保存为列值作为.csv文件 - How to save pandas dataframe generated dynmically as column values as .csv file 如何将熊猫数据框保存到压缩的csv文件中? - how can I save a Pandas dataframe into a compressed csv file? 如何在python中使用Pandas数据帧顺序创建和保存csv文件 - How to sequentially create and save csv files using Pandas dataframe in python 如何将本地 csv 文件的内容保存到“硬编码”Pandas DataFrame 中? - How to save contents of local csv file into a "hardcoded" Pandas DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM