【问题标题】:Sort a pandas Series by the index按索引对熊猫系列进行排序
【发布时间】:2013-10-09 06:59:20
【问题描述】:

我有一个名为 pd 的 Pandas 数据框,我使用以下命令提取此数据框的一列中唯一值的数量:

b = df.groupby('Region').size()

b 是 Pandas 系列对象,如下所示:

In [48]: b
Out[48]: 
Region
0          8
1         25
11         1
2         41
3         23
4         15
5         35
6         24
7         27
8         50
9         55
N         10

我正在尝试绘制这个系列的条形图,但是,我想首先根据第一列对其进行排序(因为 11 介于 1 和 2 之间),这将是 x 轴标签。我尝试传递 sort 命令,但它根据第二列中的值对系列进行排序:

b.sort()

In [48]: b
Out[54]: 
Region
11         1
0          8
N         10
4         15
3         23
6         24
1         25
7         27
5         35
2         41
8         50
9         55

那么,有没有办法根据第一列对这个系列进行排序?

【问题讨论】:

  • 这个问题是关于按 index 对 pandas Series 进行排序的。如果您想知道如何按值对系列进行排序,请参阅this post

标签: python pandas sorting


【解决方案1】:

你在找sort_index

In [80]: b.sort_values()
Out[80]: 
6     1
11    2
9     2
1     4
10    4
2     5
3     6
4     7
8     8
5     9
dtype: int64

In [81]: b.sort_index()
Out[81]: 
1     4
2     5
3     6
4     7
5     9
6     1
8     8
9     2
10    4
11    2
dtype: int64

【讨论】:

  • sort_index() 仍然给我与上面的Out[48] 相同的输出,11 仍然在 1 和 2 之间。就像 pandas 将索引值视为文本一样。我确实有 N 作为索引值之一。
【解决方案2】:

只有 1 个“列”值。第一个“列”是索引。 Docs are here

In [8]: s = Series([3,2,1],index=[1,3,2])

In [9]: s
Out[9]: 
1    3
3    2
2    1
dtype: int64

按索引排序

In [10]: s.sort_index()
Out[10]: 
1    3
2    1
3    2
dtype: int64

按值排序

In [11]: s.sort_values()
Out[11]: 
2    1
3    2
1    3
dtype: int64

【讨论】:

  • 我将此评论留给了 bdiamente 的回复,但这里也一样:sort_index() 仍然给我与上面的Out[48] 相同的输出,11 仍然在 1 和 2 之间。就像熊猫一样将索引值视为文本。我确实有 N 作为索引值之一。
  • 它们可能是文本,你是如何创建的?
  • @marillion 您的值是文本。您需要将数字转换为数字类型并使用对象 dtype 索引。然后sort_index() 会做你想做的事,尽管在没有混合数字/字符串索引的情况下很可能有更好的方法来做到这一点。
  • @Jeff df 是使用 pd.read_csv('filename.csv') 构造的,然后我使用 groupby 来计算唯一值的数量。也许数据中的“N”值导致了文本类型的索引。
  • 啊...没有看到 N,是的,当然,它会自动以这种方式显示文本。最好在 DataFrame 中输入薄,用数字替换该值,然后就可以了。
【解决方案3】:

您需要将索引转换为对象索引,因为它目前是按字典顺序排序,而不是按数字排序:

In [97]: s = read_clipboard(header=None)

In [98]: news = s.rename(columns=lambda x: ['Region', 'data'][x])

In [99]: news
Out[99]:
   Region  data
0       0     8
1       1    25
2      11     1
3       2    41
4       3    23
5       4    15
6       5    35
7       6    24
8       7    27
9       8    50
10      9    55
11      N    10

In [100]: news_converted = news.convert_objects(convert_numeric=True)

In [101]: news_converted
Out[101]:
    Region  data
0        0     8
1        1    25
2       11     1
3        2    41
4        3    23
5        4    15
6        5    35
7        6    24
8        7    27
9        8    50
10       9    55
11     NaN    10

In [102]: news_converted.loc[11, 'Region'] = 'N'

In [103]: news_converted_with_index = news_converted.set_index('Region')

In [104]: news_converted_with_index
Out[104]:
        data
Region
0.0        8
1.0       25
11.0       1
2.0       41
3.0       23
4.0       15
5.0       35
6.0       24
7.0       27
8.0       50
9.0       55
N         10

In [105]: news_converted_with_index.sort_index()
Out[105]:
        data
Region
0.0        8
1.0       25
2.0       41
3.0       23
4.0       15
5.0       35
6.0       24
7.0       27
8.0       50
9.0       55
11.0       1
N         10

很可能有更好的方法来创建您的Series,这样它就不会混合索引类型。

【讨论】:

  • @Philip Cloud 谢谢!这似乎解决了排序问题,现在我正在研究以您提到的更好的方式构建这个系列。整个事情从尝试计算 df 列中的唯一值开始,该列将“N”作为数据值之一。
猜你喜欢
  • 2020-10-20
  • 2012-08-21
  • 1970-01-01
  • 2019-06-29
  • 2018-01-20
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
相关资源
最近更新 更多