Pandas 去除索引并保存到 Excel
要将 pandas DataFrame 中的索引去掉,并将数据保存为 Excel 文件,可以使用 to_excel() 方法。这个方法接受一个参数 index,可以设置为 False 来去掉索引。
以下是一个例子:
import pandas as pd
# 创建一个DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 30, 35, 40],
'City': ['New York', 'Paris', 'London', 'Tokyo']}
df = pd.DataFrame(data)
# 将DataFrame保存为Excel文件,去掉索引
df.to_excel('output.xlsx', index=False)
这将创建一个名为 output.xlsx 的 Excel 文件,其中不包含索引。
原文地址: https://www.cveoy.top/t/topic/ph2q 著作权归作者所有。请勿转载和采集!