dataframe重排索引
时间: 2024-05-08 09:12:06 浏览: 71
DataFrame的索引
以下是两种重置DataFrame索引的方法:
1. 使用reset_ind()函数[^1]:
```python
import pandas as pd
data = [['a','3'],['b','1'],['c','2']]
df = pd.DataFrame(data)
df = df.sort_values(by=1, axis=0, ascending=False)
df = df.reset_index(drop=True)
print(df)
```
2. 使用set_index()函数重新定义索引[^2]:
```python
import pandas as pd
list1 = ['河南','安徽','山西','江西','甘肃','青海']
list2 = ['125','54.25','31.57','49.3','24.59','4.4']
data = {'省份':list1, '高考人口':list2 }
data_out = pd.DataFrame(data, index=['1','2','3','0','5','4'])
data_out = data_out.sort_index()
print(data_out)
```
阅读全文