pandas 以某列排序
时间: 2023-08-09 14:05:58 浏览: 119
要使用pandas对DataFrame按照某列进行排序,可以使用sort_values()方法。该方法接受一个参数,即要排序的列名。下面是一个示例代码:
```python
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 3), index=\[1, 6, 8, 2, 3, 5, 7, 4, 0, 9\], columns=\['c2', 'c1', 'c3'\])
print(df)
print('\n')
df2 = df.sort_values(by='c1')
print(df2)
```
在这个例子中,我们创建了一个DataFrame对象df,并指定了索引和列名。然后使用sort_values()方法按照'c1'列进行排序,将结果赋值给df2。最后打印df2,即按照'c1'列排序后的DataFrame。\[1\]
#### 引用[.reference_title]
- *1* [Pandas-按照某行标签或某列标签进行排序](https://blog.csdn.net/Leexin_love_Ling/article/details/131047579)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [Pandas 实用技能,将列(column)排序的几种方法](https://blog.csdn.net/dQCFKyQDXYm3F8rB0/article/details/125075906)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文