dataframe按照行标题排序
时间: 2023-12-09 11:45:33 浏览: 80
要按照行标题对DataFrame进行排序,可以使用sort_index()方法。这个方法会根据行索引的值对DataFrame进行排序。例如,假设有一个未排序的DataFrame df,可以使用df.sort_index()来按照行标题进行排序。下面是一个示例代码:
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')
df_sorted = df.sort_index()
print(df_sorted)
这段代码首先创建了一个10行3列的DataFrame,然后使用sort_index()方法对DataFrame按照行标题进行排序。最后,打印出排序后的DataFrame df_sorted。
#### 引用[.reference_title]
- *1* [dataframe排序、重新设置索引、获取行数和列数,numpy.zeros、](https://blog.csdn.net/weixin_39683978/article/details/111516053)[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-按照某行标签或某列标签进行排序](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]
[ .reference_list ]
阅读全文