Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否将行索引重置为从0以外的数字开始?_Python_Csv_Pandas_Indexing - Fatal编程技术网

Python 是否将行索引重置为从0以外的数字开始?

Python 是否将行索引重置为从0以外的数字开始?,python,csv,pandas,indexing,Python,Csv,Pandas,Indexing,目前,我正在尝试读入一个.csv文件,然后使用to_html()创建一个侧面有索引的表。此处的所有代码行: import pandas as pd df = pd.read_csv('file.csv') df.to_html('example.html') 正如所料,我目前得到: Year Population Annual Growth Rate 0 1950 2557628654 1.458 1 1951 2594919657

目前,我正在尝试读入一个.csv文件,然后使用to_html()创建一个侧面有索引的表。此处的所有代码行:

import pandas as pd  
df = pd.read_csv('file.csv')  
df.to_html('example.html')  
正如所料,我目前得到:

   Year  Population      Annual Growth Rate 
0  1950  2557628654      1.458   
1  1951  2594919657      1.611  
2  1952  2636732631      1.717  
3  1953  2681994386      1.796   
4  1954  2730149884      1.899 
但是,我希望从2开始索引,而不是从0开始。例如:

   Year  Population      Annual Growth Rate 
2  1950  2557628654      1.458   
3  1951  2594919657      1.611  
4  1952  2636732631      1.717  
5  1953  2681994386      1.796   
6  1954  2730149884      1.899   
df.index = df.index + 2
我知道我可以通过在.csv文件中添加两个虚拟行,然后使用df.ix[]删除它们来实现这个结果,但我不想这样做。
是否有一种方法可以将索引更改为从0以外的位置开始,而不必在.csv文件中添加或删除行


谢谢

我知道这看起来像一个黑客,但如果只是改变索引序列呢。例如:

   Year  Population      Annual Growth Rate 
2  1950  2557628654      1.458   
3  1951  2594919657      1.611  
4  1952  2636732631      1.717  
5  1953  2681994386      1.796   
6  1954  2730149884      1.899   
df.index = df.index + 2

不是黑客!这是解决OP问题最直接、最直观的方法。我会选择你所做的更加美观的版本
df.index+=2
。但那只是我,谢谢你!你真棒!