DataFrame排序,单列排序,多列排序
import pandas as pd # 创建示例 DataFrame data = { 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [30, 25, 35], 'Salary': [50000, 60000, 45000] } df = pd.DataFrame(data) # 按照 'Age' 列升序排序 sorted_df = df.sort_values(by='Age', ascending=True) print("Original DataFrame:") print(df) print("\nSorted DataFrame:") print(sorted_df)
import pandas as pd # 创建示例 DataFrame data = { 'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [30, 25, 35, 25], 'Salary': [50000, 60000, 45000, 55000] } df = pd.DataFrame(data) # 按照 'Age' 列升序,然后按照 'Salary' 列降序排序 sorted_df = df.sort_values(by=['Age', 'Salary'], ascending=[True, False]) print("Original DataFrame:") print(df) print("\nSorted DataFrame:") print(sorted_df)
本文来自博客园,作者:OTAKU_nicole,转载请注明原文链接:https://www.cnblogs.com/nicole-zhang/p/17614006.html