在Python中对 Pandas 系列进行排序
系列是一个一维标签数组,能够保存整数、字符串、浮点数、 Python对象等类型的数据。轴标签统称为索引。
现在,让我们看一个对 Pandas Series 进行排序的程序。
为了对熊猫系列进行排序,使用Series.sort_values()方法。
Syntax: Series.sort_values(axis=0, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’)Sorted
Returns: Sorted series
示例 1:按升序对数字系列进行排序。
Python3
# importing pandas as pd
import pandas as pd
# define a numeric series
s = pd.Series([100, 200, 54.67,
300.12, 400])
# print the unsorted series
s
Python3
# sorting series s with
# s.sort_value() method in
# ascending order
sorted_series = s.sort_values(ascending
= True)
# print the sorted series
sorted_series
Python3
# importing pandas as pd
import pandas as pd
# define a numeric series
s = pd.Series([100, 200, 54.67,
300.12, 400])
# print the unsorted series
s
Python3
# sorting the series s with
# s.sort_values() method
# in descending order
sorted_series = s.sort_values(ascending
= False)
# printing the sorted series
sorted_series
Python3
# importing pandas as pd
import pandas as pd
#d efine a string series s
s = pd.Series(["OS","DBMS","DAA",
"TOC","ML"])
# print the unsorted series
s
Python3
# sorting the series s with
# s.sort_values() method
# in ascending order
sorted_series = s.sort_values(ascending
= True)
# printing the sorted series
sorted_series
Python3
# importing numpy as np
import numpy as np
# importing pandas as pd
import pandas as pd
# define a numeric series
# s with a NaN
s = pd.Series([np.nan, 1, 3,
10, 5])
# print the unsorted series
s
Python3
# sorting the series s with
# s.sort_values() method in
# descending order and inplace
s.sort_values(ascending = False,
inplace = True)
# printing the sorted series
s
Python3
# importing numpy as np
import numpy as np
# importing pandas as pd
import pandas as pd
# define a numeric series
# s with a NaN
s = pd.Series([np.nan, 1, 3,
10, 5])
# print the unsorted series
s
Python3
# sorting the series s with
# s.sort_values() method in
# ascending order with na
# position at first
sorted_series = s.sort_values(na_position =
'first')
# printing the sorted series
sorted_series
输出:
现在我们将使用Series.sort_values()方法按升序对数字系列进行排序。
Python3
# sorting series s with
# s.sort_value() method in
# ascending order
sorted_series = s.sort_values(ascending
= True)
# print the sorted series
sorted_series
输出:
从输出中,我们可以看到数字系列是按升序排序的。
示例 2:按降序对数字系列进行排序。
Python3
# importing pandas as pd
import pandas as pd
# define a numeric series
s = pd.Series([100, 200, 54.67,
300.12, 400])
# print the unsorted series
s
输出:
现在我们将使用Series.sort_values()方法按降序对数字系列进行排序。
Python3
# sorting the series s with
# s.sort_values() method
# in descending order
sorted_series = s.sort_values(ascending
= False)
# printing the sorted series
sorted_series
输出:
从输出中,我们可以看到数字系列是按降序排列的。
示例 3:对一系列字符串进行排序。
Python3
# importing pandas as pd
import pandas as pd
#d efine a string series s
s = pd.Series(["OS","DBMS","DAA",
"TOC","ML"])
# print the unsorted series
s
输出:
现在我们将使用Series.sort_values()方法对一系列字符串进行排序。
Python3
# sorting the series s with
# s.sort_values() method
# in ascending order
sorted_series = s.sort_values(ascending
= True)
# printing the sorted series
sorted_series
输出:
从输出中,我们可以看到字符串系列是按字典顺序升序排序的。
示例 4:就地排序值。
Python3
# importing numpy as np
import numpy as np
# importing pandas as pd
import pandas as pd
# define a numeric series
# s with a NaN
s = pd.Series([np.nan, 1, 3,
10, 5])
# print the unsorted series
s
输出:
现在我们将使用Series.sort_values()方法对值进行就地排序
Python3
# sorting the series s with
# s.sort_values() method in
# descending order and inplace
s.sort_values(ascending = False,
inplace = True)
# printing the sorted series
s
输出:
输出显示 Pandas 系列中的就地排序。
示例 5:通过将 NaN 放在首位来对系列中的值进行排序。
Python3
# importing numpy as np
import numpy as np
# importing pandas as pd
import pandas as pd
# define a numeric series
# s with a NaN
s = pd.Series([np.nan, 1, 3,
10, 5])
# print the unsorted series
s
输出:
现在我们将使用Series.sort_values()方法通过将 NaN 放在首位来对系列中的值进行排序。
Python3
# sorting the series s with
# s.sort_values() method in
# ascending order with na
# position at first
sorted_series = s.sort_values(na_position =
'first')
# printing the sorted series
sorted_series
输出:
输出显示 NaN(不是数字)值在排序序列中排在第一位。