pandas各种排序函数
时间: 2023-11-29 13:46:54 浏览: 70
以下是Pandas中常用的各种排序函数:
1. Series数据的排序:
- 按值排序:sort_values()函数
```python
import pandas as pd
s = pd.Series([3, 1, 4, 2, 5])
s_sorted = s.sort_values()
print(s_sorted)
```
- 按索引排序:sort_index()函数
```python
import pandas as pd
s = pd.Series([3, 1, 4, 2, 5], index=['c', 'a', 'd', 'b', 'e'])
s_sorted = s.sort_index()
print(s_sorted)
```
2. DataFrame数据的排序:
- 按值排序:sort_values()函数
```python
import pandas as pd
fpath="./datas/600033.csv"
df=pd.read_csv(fpath)
df_sorted = df.sort_values(by='date', ascending=True)
print(df_sorted)
```
- 按索引排序:sort_index()函数
```python
import pandas as pd
fpath="./datas/600033.csv"
df=pd.read_csv(fpath)
df_sorted = df.sort_index()
print(df_sorted)
```
- 按多列排序:sort_values()函数
```python
import pandas as pd
fpath="./datas/600033.csv"
df=pd.read_csv(fpath)
df_sorted = df.sort_values(by=['date', 'open'], ascending=[True, False])
print(df_sorted)
```
阅读全文