简体   繁体   English

将python生成的输出导出到excel文件

[英]to export the output generated in python to excel file

Given below is some part of data, I want to export the data from output box in Jupyter Notebook to the excel file.下面给出了部分数据,我想将 Jupyter Notebook 中输出框中的数据导出到 excel 文件中。 The data is quite huge like 10k lines.数据非常庞大,就像 10k 行。

17:38:00 17:38:01 17:38:02 17:38:03 17:38:04 . 17:38:00 17:38:01 17:38:02 17:38:03 17:38:04 。 . . . . Following is my code:-以下是我的代码:-

enter code here
import pandas as pd
import numpy as np
load_var=pd.read_csv(r'path')

# Select the dataframe
col_var=load_var['End Seconds']

# Converting the dataframe to array

a=col_var.values.tolist()


# define the function for time conversion
def convert(seconds): 
seconds = seconds % (24 * 3600) 
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60

return "%d:%02d:%02d" % (hour, minutes, seconds) 


for n in a:
print(convert(n))

1)Please suggest the addition to be made in the code. 1)请建议在代码中进行添加。

Thank you.谢谢你。

使用df.to_excel()导出到 excel 文件

df.to_excel(r'Path to store the exported excel file\File Name.xlsx', index = False)

You have to apply your function to the series that you want to modify like this:您必须将您的功能应用到您要修改的系列,如下所示:

load_var = pd.read_csv(r'path')    
load_var['End Seconds'] = load_var['End Seconds'].apply(convert)

Now you have the converted data in memory which can be saved to Excel using df.to_excel() as Fahmi mentioned.现在您在内存中拥有转换后的数据,可以使用 Fahmi 提到的df.to_excel()将其保存到 Excel 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM