python - 我想将我的目标文件保存为 .CSV 而不是 xlsx
问题描述
我想将我的目标文件保存在 .CSV 而不是 xlsx 中,此代码运行良好,但我无法将输出文件保存为 .CSV 我在 google 上查看,但我找不到任何东西
# Main function
def classement_top(FILE_SOURCE,FILE_DESTINATION):
df = pd.read_csv(FILE_SOURCE)
liste_partant = list(set(df['comp']))
liste_filtre_partant = []
for partant in liste_partant:
source = 'df_partcccant_' + str(partant)
liste_filtre_partant.append(source)
vars()[source] = df[df['comp'] == partant]
vars()[source] = vars()[source].nlargest(5, ['total_teste'])
top1 = 0
top2 = 0
top3 = 0
top4 = 0
top5 = 0
for elem in list(vars()[source]['cl']):
if elem == '1er':
top1 = 1
if elem == '2e':
top2 = 1
if elem == '3e':
top3 = 1
if elem == '4e':
top4 = 1
if elem == '5e':
top5 = 1
top5 = top5 + top4 + top3 + top2 + top1
top4 = top4 + top3 + top2 + top1
top3 = top3 + top2 + top1
top2 = top2 + top1
for i in list(vars()[source].index):
vars()[source].at[i,'top1'] = top1
vars()[source].at[i,'top2'] = top2
vars()[source].at[i,'top3'] = top3
vars()[source].at[i,'top4'] = top4
vars()[source].at[i,'top5'] = top5
liste_variable = []
for elem in liste_filtre_partant:
liste_variable.append(vars()[elem])
final_df = pd.concat(liste_variable)
final_df = final_df.sort_values(by=['jour', 'comp'], ascending=True)
final_df.to_excel(FILE_DESTINATION, index=False)
使用我们要保存文件的位置启动函数 FILE_DESTINATION 这段代码运行良好,但我无法将输出文件保存为 .CSV
FILE_DESTINATION = "C:/Users/Utilisateur/Documents/PMU/EXPORT/sorti_teste_1_suite.xlsx"
文件源
FILE_SOURCE = "C:/Users/Utilisateur/Documents/PMU/EXPORT/teste_1_suite.csv"
classement_top(FILE_SOURCE,FILE_DESTINATION)
解决方案
利用final_df.to_excel(FILE_DESTINATION, index=False)
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html
(移动评论回答)
推荐阅读
- python-3.6 - seaborn stripplot ValueError:无法解释输入'OS'
- java - 在 Spring 中使用 ContextStartedEvent 将过滤器添加到 servletcontext
- c++ - 使用 \0 填充字符串格式?
- javascript - 反应片段未在功能中呈现
- gradle - 无法使用 gradle --scan 选项构建 gradle 项目
- python-3.x - 导入日历 0
- java - 安装Tomcat 8后Java导入util.Arrays失败,如何解决?
- xamarin.forms - 新创建的 Xamarin.Forms 项目未构建
- c# - 如何使用 Get 方法包含 FK 表
- python - 选择将 Dask 数组保存到 hdf5 文件的模式