在保持顺序的同时从列表中选择数据

selecting data from list whiles keeping the order

试图从列表中 select 子集,但是在 selection

之后顺序颠倒了

尝试使用 pandas isin

df.mon =[1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,...] 
 # selecting 
 results = df[df.month.isin([10,11,12,1,2,3])]
 print(results.mon]
 mon = [1,2,3,10,11,12, 1,2,3,10,11,12,...]
 desired results
 mon= [10,11,12,1,2,3,10,11,12,1,2,3,...]

 # sorting results in this
 mon = [1,1,2,2,3,3,10,10,11,11,12,12] and I dont want that either

 thanks for the help

我最常使用基本 python 列表,因此我已将 df 转换为列表。

数据

数据显示在这样的xlsx文件中。 输入是一个 xlsx 文档,它只有两次 1、2、.. 12、1、2、.. 12,"Values" 从 90 开始,一直按 10 计数,直到第二个 12。

进程

import pandas as pd

df = pd.read_excel('Book1.xlsx')
arr = df['Column'].tolist()
arr2 = df['Values'].tolist()

monthsofint = [10, 11, 12, 1, 2, 3]

locs = []

dictor = {}
for i in range(len(monthsofint)):
    dictor[monthsofint[i]] = []

for i in range(len(monthsofint)):  # !! Assumption !!
    for j in range(len(arr)):
        if monthsofint[i] == arr[j]:
            dictor[monthsofint[i]].append(j)

newlist = []
newlist2 = []
for i in range(len(dictor[monthsofint[0]])):
    for j in range(len(monthsofint)):
        newlist.append(arr[dictor[monthsofint[j]][i]])
        newlist2.append(arr2[dictor[monthsofint[j]][i]])

print(newlist)
print(newlist2)

输出:[10, 11, 12, 1, 2, 3, 10, 11, 12, 1, 2, 3][180, 190, 200, 90, 100, 110, 300, 310, 320, 210, 220, 230]

关于假设的说明:所做的假设是文件中每年始终有 12 个月。

在你的情况下,我们使用 Categorical + cumcount

#results = df[df.mon.isin([10, 11, 12, 1, 2, 3])].copy()
results.mon=pd.Categorical(results.mon,[10,11,12,1,2,3])
s=results.sort_values('mon')
s=s.iloc[s.groupby('mon').cumcount().argsort()]
s
Out[172]: 
   mon
9   10
10  11
11  12
0    1
1    2
2    3
21  10
22  11
23  12
12   1
13   2
14   3

我认为您可以获取每个类别的内容,然后使用 izip_longest 压缩这些列表。

所以我从另一个来源找到了一种相对容易和简单的方法

对于那些可能感兴趣的人:

  df[(df.index > 4) & (df.month.isin([10, 11, 12, 1, 2, 3]))]