在中搜索优化的选择Pandas.datafram

2024-09-30 01:21:05 发布

您现在位置:Python中文网/ 问答频道 /正文

在一个表中选择某些行最有效的方法是什么1.数据帧,包含N列(字符串、整数和浮点),根据此选择:

  • 遍历2列(整数)的所有组合。你知道吗
  • 对于每个不同的组合,在第三列(float)中只保留一行(即所有列)的最小值

例如,对于第三列为tete的(titi,tutu)组合:

  toto  tata  titi  tutu  tete
0    a    18   600   700   4.5
1    b    18   600   800  10.1
2    c    18   600   700  12.6
3    d     3   300   400   3.4
4    a    16   900  1000   6.0
5    a    18   600   800  10.1
6    c     3   300   400   3.0
7    a    16   900  1000   6.0

必须给出:

    toto  tata  titi  tutu  tete
0    a    18   600   700   4.5
1    b    18   600   800  10.1
4    a    16   900  1000   6.0
6    c     3   300   400   3.0

目前,我从以下代码开始:

import pandas
indicesToKeep = []
indicesToRemove = []
reader = pandas.read_csv('/Users/steph/work/perso/sof/test.csv')
columns = reader.columns
for i in reader['titi'].unique():
    #temp = reader[[:]].query('titi == i')#does not work !
    temp = reader.loc[(reader.titi == i),columns]
    for j in temp['tutu'].unique():
        temp2 = temp.loc[(temp.tutu == j),columns]
        minimum = min(temp2.tete)
        indicesToKeep.append(min(
                temp2[temp2.tete==minimum].index.tolist()))
################
# compute the complement of indicesToKeep
#but I don't remember the pythonic syntax
for i in range(len(reader)):
    if i not in indicesToKeep:
        indicesToRemove.append(i)
############################
reader = reader.drop(indicesToRemove)            

注:

  • 我敢肯定这是没有优化。你知道吗
  • 我使用旧的'loc'方法,因为我不知道如何使用'query'

Tags: columns方法infor整数tititemploc
2条回答

您可以按两列tititutu分组,然后获得第三行tete的最小值的行索引。完成后,只需查找行。你知道吗

df.loc[df.groupby(["titi", "tutu"])["tete"].idxmin()]

这将返回输出

  toto  tata  titi  tutu  tete
6    c     3   300   400   3.0
0    a    18   600   700   4.5
1    b    18   600   800  10.1
4    a    16   900  1000   6.0

这是如上所述的期望输出。你知道吗

groupby将确保保留这两列的所有可能组合。你知道吗

IIUCsort_values+drop_duplicates,如果你起诉pandas试图不使用for循环,大多数时候它比矢量化方法慢

df.sort_values('tete').drop_duplicates(['titi','tutu']).sort_index()
Out[583]: 
  toto  tata  titi  tutu  tete
0    a    18   600   700   4.5
1    b    18   600   800  10.1
4    a    16   900  1000   6.0
6    c     3   300   400   3.0

相关问题 更多 >

    热门问题