danilshik Asked:2020-01-11 20:55:59 +0000 UTC2020-01-11 20:55:59 +0000 UTC 2020-01-11 20:55:59 +0000 UTC 从 1 开始的 Pandas 索引 772 在 Excel 中保存数据时,记录的索引从 0 开始 pd.DataFrame(products).to_excel(config["output_filename"], index=True) 有没有办法从1开始索引? python 1 个回答 Voted Best Answer MaxU - stop genocide of UA 2020-01-11T21:00:53Z2020-01-11T21:00:53Z 试试这样: import numpy as np (pd.DataFrame(products, index=np.arange(1, len(products)+1)) .to_excel(config["output_filename"], index=True)) 从 1 开始索引的几种方法: DF 示例: In [7]: df = pd.DataFrame(np.arange(12).reshape(4,3), columns=list("abc")) In [8]: df Out[8]: a b c 0 0 1 2 1 3 4 5 2 6 7 8 3 9 10 11 方式1 df.index += 1 方式2 df = df.set_index(np.arange(1, len(df)+1)) 结果 In [10]: df Out[10]: a b c 1 0 1 2 2 3 4 5 3 6 7 8 4 9 10 11
试试这样:
从 1 开始索引的几种方法:
DF 示例:
方式1
方式2
结果