如何将 Pandas DataFrame 保存到 excel 文件? [英] how to save a pandas DataFrame to an excel file?
本文介绍了如何将 Pandas DataFrame 保存到 excel 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从 Web 源加载数据并将其另存为 Excel 文件,但不知道如何操作.我该怎么办?
I am trying to load data from the web source and save it as a Excel file but not sure how to do it. What should I do?
import requests
import pandas as pd
import xmltodict
url = "https://www.kstan.ua/sitemap.xml"
res = requests.get(url)
raw = xmltodict.parse(res.text)
data = [[r["loc"], r["lastmod"]] for r in raw["urlset"]["url"]]
print("Number of sitemaps:", len(data))
df = pd.DataFrame(data, columns=["links", "lastmod"])
推荐答案
df.to_csv("output.csv", index=False)
或
df.to_excel("output.xlsx")
这篇关于如何将 Pandas DataFrame 保存到 excel 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文