pandas dataframe dataframe.sort_values() 函数
pandas dataframe.sort_values() 方法将调用者 dataframe 沿任一索引的指定列中的值按升序或降序排序。
pandas.dataframe.sort_values() 语法
dataframe.sort_values(
by,
axis=0,
ascending=true,
inplace=false,
kind="quicksort",
na_position="last",
ignore_index=false,
)
参数
by |
要排序的名称或名称列表 |
axis |
沿行(axis=0)或列(axis=1)排序 |
ascending |
按升序排序(ascending=true)或降序排序(ascending=false) |
inplace |
布尔型。如果为 true,就地修改调用者 dataframe |
kind |
排序算法。默认为 quicksort |
na_position |
将 nan 值放在开头(na_position=first)或结尾(na_position=last) |
ignore_index |
布尔型。如果是 true,则忽略原始 dataframe 中的索引,默认值是 false,即使用索引。默认值是 false,表示使用索引。 1.0.0 版本新增 |
返回值
如果 inplace 为 true,则返回排序后的 dataframe;否则为 none。
示例代码:使用 pandas 对 dataframe 进行排序 pandas.dataframe.sort_values() 基于单列进行排序
import pandas as pd
dates=['april-10',
'april-11',
'april-12',
'april-13',
'april-14',
'april-16']
sales=[200,300,400,200,300,300]
prices=[3, 1, 2, 4,3,2]
df = pd.dataframe({'date':dates ,
'sales':sales ,
'price': prices})
print("before sorting:")
print(df)
sorted_df=df.sort_values(by=['price'])
print("after sorting:")
print(sorted_df)
输出:
before sorting:
date sales price
0 april-10 200 3
1 april-11 300 1
2 april-12 400 2
3 april-13 200 4
4 april-14 300 3
5 april-16 300 2
after sorting:
date sales price
date sales price
1 april-11 300 1
2 april-12 400 2
5 april-16 300 2
0 april-10 200 3
4 april-14 300 3
3 april-13 200 4
它根据 price 列中的值按升序(默认)对 dataframe df 进行排序。
排序后的 dataframe 中的索引与原始 dataframe 中的索引保持一致。
如果你喜欢在排序后的 dataframe 中使用新的索引列,那么你可以设置 ignore_index(从 1.0.0 版本引入)为 true。
import pandas as pd
dates = ["april-10", "april-11", "april-12", "april-13", "april-14", "april-16"]
sales = [200, 300, 400, 200, 300, 300]
prices = [3, 1, 2, 4, 3, 2]
df = pd.dataframe({"date": dates, "sales": sales, "price": prices})
print("before sorting:")
print(df)
sorted_df = df.sort_values(by=["price"], ignore_index=true)
print("after sorting:")
输出:
before sorting:
date sales price
0 april-10 200 3
1 april-11 300 1
2 april-12 400 2
3 april-13 200 4
4 april-14 300 3
5 april-16 300 2
after sorting:
date sales price
0 april-11 300 1
1 april-12 400 2
2 april-16 300 2
3 april-10 200 3
4 april-14 300 3
5 april-13 200 4
在这里,我们使用 ignore_index=true 为行分配新的索引,并忽略原来 dataframe 的索引。
示例代码:使用 pandas dataframe.sort_values() 基于多列对 dataframe 进行排序
import pandas as pd
dates=['april-10',
'april-11',
'april-12',
'april-13',
'april-14',
'april-16']
sales=[200,300,400,200,300,300]
prices=[3, 1, 2, 4,3,2]
df = pd.dataframe({'date':dates ,
'sales':sales ,
'price': prices})
print("before sorting:")
print(df)
df.sort_values(by=['sales','price'],
ignore_index=true,
inplace=true)
print("after sorting:")
print(df)
输出:
before sorting:
date sales price
0 april-10 200 3
1 april-11 300 1
2 april-12 400 2
3 april-13 200 4
4 april-14 300 3
5 april-16 300 2
after sorting:
date sales price
0 april-10 200 3
1 april-13 200 4
2 april-11 300 1
3 april-16 300 2
4 april-14 300 3
5 april-12 400 2
在这里,首先按升序对 sales 进行排序,然后对每个 sales 的 price 也按升序进行排序。
在 df 中,200 是 sales 列的最小值,3 是 sales 值为 200 的 price 列的最小值。
所以,sales 列中有 200,price 列中有 3 的行排在最前面。
由于 inplace=true,调用 sort_values() 函数后,原 dataframe 被就地修改。
示例代码:用 pandas dataframe.sort_values() 对 dataframe 进行降序排序
import pandas as pd
dates=['april-10',
'april-11',
'april-12',
'april-13',
'april-14',
'april-16']
sales=[200,300,400,200,300,300]
prices=[3, 1, 2, 4,3,2]
df = pd.dataframe({'date':dates ,
'sales':sales ,
'price': prices})
print("before sorting:")
print(df)
sorted_df=df.sort_values(by=['sales'],
ignore_index=true,
ascending=false)
print("after sorting:")
print(sorted_df)
输出:
before sorting:
date sales price
0 april-10 200 3
1 april-11 300 1
2 april-12 400 2
3 april-13 200 4
4 april-14 300 3
5 april-16 300 2
after sorting:
date sales price
0 april-12 400 2
1 april-11 300 1
2 april-14 300 3
3 april-16 300 2
4 april-10 200 3
5 april-13 200 4
它按照 sales 列的数值降序对 dataframe df 进行排序。
400 是 sales 列中的最大值,因此该条目将被置于顶端,其他行也将相应地进行排序。
示例代码:使用 pandas dataframe.sort_values() 对 dataframe 进行排序,将 nan 放在开头
import pandas as pd
dates=['april-10',
'april-11',
'april-12',
'april-13',
'april-14',
'april-16']
sales=[200,300,400,200,300,300]
prices=[3, 1, 2, 4,3,2]
df = pd.dataframe({'date':dates ,
'sales':sales ,
'price': prices})
print("before sorting:")
print(df)
sorted_df=df.sort_values(by=['price'],ignore_index=true,na_position='first')
print("after sorting:")
print(sorted_df)
输出:
before sorting:
date sales price
0 april-10 200 nan
1 april-11 300 1.0
2 april-12 400 2.0
3 april-13 200 4.0
4 april-14 300 3.0
5 april-16 300 nan
after sorting:
date sales price
0 april-10 200 nan
1 april-16 300 nan
2 april-11 300 1.0
3 april-12 400 2.0
4 april-14 300 3.0
5 april-13 200 4.0
在默认情况下,nan 值在排序后被放在 dataframe 的最后。
但是通过设置 na_position=first,我们可以将 nan 值放在 dataframe 的开头。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串

