Pandas 基于值的索引对序列行重新排序

Pandas 基于值的索引对序列行重新排序,pandas,indexing,series,Pandas,Indexing,Series,我有一个大熊猫系列,所以优化是关键 pd.Series(['I like apples', 'They went skiing vacation', 'Apples are tasty', 'The skiing was great'], dtype='string') 0 I like apples 1 They went skiing vacation 2 Apples are tasty 3 The skiing

我有一个大熊猫系列,所以优化是关键

pd.Series(['I like apples', 'They went skiing vacation', 'Apples are tasty', 'The skiing was great'], dtype='string')

0                I like apples
1    They went skiing vacation
2             Apples are tasty
3         The skiing was great
dtype: string
假设这些行是字符串列表,即第0行是['i','like','apples']

我想获取say'apples'的索引,并根据该索引的值对行进行重新排序。在本例中,该系列类似于:

2             Apples are tasty
0                I like apples
1    They went skiing vacation
3         The skiing was great
dtype: string
因为第2行中“apples”(忽略大小写敏感度)的索引为0。

使用

列表理解和列举的另一个想法:

a = [next(iter(i for i, j in enumerate(x.split()) if j.lower() == 'apples'), len(s)*10) for x in s]
print (a)
[2, 40, 0, 40]

s = s.loc[np.array(a).argsort()]
print (s)
2             Apples are tasty
0                I like apples
1    They went skiing vacation
3         The skiing was great
dtype: string

抱歉,耶兹雷尔,我的问题不清楚。行由字符串列表组成。(我相应地编辑了我的问题)len(s)*10的原因是什么?@user270199为了正确的顺序,我需要一些大数字,而不是最大整数,我使用这个技巧
a = [next(iter(i for i, j in enumerate(x.split()) if j.lower() == 'apples'), len(s)*10) for x in s]
print (a)
[2, 40, 0, 40]

s = s.loc[np.array(a).argsort()]
print (s)
2             Apples are tasty
0                I like apples
1    They went skiing vacation
3         The skiing was great
dtype: string