Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫群比后如何找回索引_Python_Pandas - Fatal编程技术网

Python 熊猫群比后如何找回索引

Python 熊猫群比后如何找回索引,python,pandas,Python,Pandas,我试图从groupby之后的每个组中的第一条记录中找到具有最大值的记录,并从原始数据帧中删除该记录 import pandas as pd df = pd.DataFrame({'item_id': ['a', 'a', 'b', 'b', 'b', 'c', 'd'], 'cost': [1, 2, 1, 1, 3, 1, 5]}) print df t = df.groupby('item_id').first() #lost track of th

我试图从groupby之后的每个组中的第一条记录中找到具有最大值的记录,并从原始数据帧中删除该记录

import pandas as pd
df = pd.DataFrame({'item_id': ['a', 'a', 'b', 'b', 'b', 'c', 'd'], 
                   'cost': [1, 2, 1, 1, 3, 1, 5]})
print df 
t = df.groupby('item_id').first() #lost track of the index
desired_row = t[t.cost == t.cost.max()]
#delete this row from df

         cost
item_id      
d           5
我需要跟踪
所需的\u行
,并从
df
中删除该行,然后重复该过程


查找和删除所需行的最佳方法是什么?

我不确定一般方法,但这在您的情况下会起作用,因为您是每组的第一个项目(最后一个项目也很容易)。事实上,由于分割骨料联合收割机的一般性质,我认为如果不自己动手,这是不容易实现的

gb = df.groupby('item_id', as_index=False)
>>> gb.groups  # Index locations of each group.
{'a': [0, 1], 'b': [2, 3, 4], 'c': [5], 'd': [6]}

# Get the first index location from each group using a dictionary comprehension.
subset = {k: v[0] for k, v in gb.groups.iteritems()}
df2 = df.iloc[subset.values()]
# These are the first items in each groupby.
>>> df2
   cost item_id
0     1       a
5     1       c
2     1       b
6     5       d

# Exclude any items from above where the cost is equal to the max cost across the first item in each group.
>>> df[~df.index.isin(df2[df2.cost == df2.cost.max()].index)]
   cost item_id
0     1       a
1     2       a
2     1       b
3     1       b
4     3       b
5     1       c
或者不使用

考虑这个df,它只包含很少的行

pd.DataFrame({'item_id': ['a', 'a', 'b', 'b', 'b', 'c', 'd', 'd','d'], 
               'cost': [1, 2, 1, 1, 3, 1, 5,1,7]})

df[~df.cost.isin(df.groupby('item_id').first().max().tolist())]

    cost    item_id
0   1       a
1   2       a
2   1       b
3   1       b
4   3       b
5   1       c
7   1       d
8   7       d
试试这个

import pandas as pd
df = pd.DataFrame({'item_id': ['a', 'a', 'b', 'b', 'b', 'c', 'd'],
                   'cost': [1, 2, 1, 1, 3, 1, 5]})
t=df.drop_duplicates(subset=['item_id'],keep='first')
desired_row = t[t.cost == t.cost.max()]
df[~df.index.isin([desired_row.index[0]])]

Out[186]: 
   cost item_id
0     1       a
1     2       a
2     1       b
3     1       b
4     3       b
5     1       c

概述:使用字典创建数据帧。按项目id分组并找到最大值。在分组的数据帧上枚举,并使用作为数值的键返回alpha索引值。如果需要,创建一个结果数据框

   df_temp = pd.DataFrame({'item_id': ['a', 'a', 'b', 'b', 'b', 'c', 'd'], 
               'cost': [1, 2, 1, 1, 3, 1, 5]})

   grouped=df_temp.groupby(['item_id'])['cost'].max()

   result_df=pd.DataFrame(columns=['item_id','cost'])

   for key, value in enumerate(grouped):
     index=grouped.index[key]
     result_df=result_df.append({'item_id':index,'cost':value},ignore_index=True)

   print(result_df.head(5))

尝试使用df.drop API如何知道从
df
中删除哪个索引?谢谢,但它将删除
项目id
列中包含
d
的所有行。我只想删除包含max
cost
的行。此解决方案不会删除与“d”对应的所有成本。pl查看编辑它不适用于此df:
df=pd.DataFrame({'item_id':['a','a','b','b','b','c','d','d','d'],'cost':[5,2,1,7,3,1,5,1,7]})
因为您正在删除成本等于第一项的最大成本的所有行。@Alexander抱歉,我不明白您的意思。我发布的输出数据帧在索引6处只少了一行。除非我理解这个问题wrong@learner,如果有两行具有最大值,则预期的行为是什么。假设索引0处的成本也是5?@Vaishali我只需要删除一行。删除所有行对我来说是不可行的。将最后一行转换为
df[~df.index.isin([desired_row.index[0]])]
对我有用。@leaner edited~,IDK为了过滤和更新,您希望保留哪一行