How can I export the Python execution results as a spreadsheet?

To export the output of Python as a table, you can use the pandas library. Here is an example:

import pandas as pd

# 创建一个包含数据的字典
data = {'姓名': ['张三', '李四', '王五'],
        '年龄': [20, 25, 30],
        '性别': ['男', '女', '男']}

# 将字典转换为DataFrame
df = pd.DataFrame(data)

# 将DataFrame保存为Excel文件
df.to_excel('output.xlsx', index=False)

In this example, a dictionary containing data is first created using the pandas library. Then, the dictionary is converted into a DataFrame object. Lastly, the data is saved as an Excel file named output.xlsx using the to_excel method of the DataFrame.

You can also save the DataFrame as a table in a different format, such as a CSV file, by using the corresponding method, like to_csv.

Before running this example, make sure that the pandas library is installed. You can install it using the command “pip install pandas”.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds