2017-06-18 142 views
0

我是编程和python的新手。我有一个由熊猫模块创建的数据框。数据集的索引列是“Rho”,“Capacity”和“Model-Version”。基于这些因素,我进行了一些模拟,并找到了每次运行5次的收入。这些列表示“平均”,“下限”和“上限”。基于行比较删除python熊猫DataFrame中的特定行

现在我想为每个特定的Rho和容量找到最好的收入。所以我应该比较不同模型的上限和下限。如果一行的下界高于另一个上界的值,我应该删除上界小的那一行。

这里是我当前的代码:

from pandas import * 

df_rev = DataFrame.from_csv(path="revenue_total.csv", index_col=[3, 4, 5]) 

print(df_rev.iloc[0][2]) 

# removing those revenues in a class that are low: 
for index1, row1 in df_rev.iterrows(): 
    for index2, row2 in df_rev.iterrows(): 
     if index1[0] is index2[0] and index1[1] is index2[1]: 
      if row1[1] > row2[2]: 
       df_rev = df_rev.drop(df_rev.index[index2]) 
      elif row2[1] > row1[2]: 
       df_rev = df_rev.drop(df_rev.index[index1]) 

print(df_rev) 

但是,我知道,这是行不通的。有谁知道我应该怎么做?

谢谢

回答

0

您应该找到最大的收入。

a = [['a','a',1,5],['a','a',3,4],['a','b',5,6],['b','c',7,8]] 
df = pd.DataFrame(a) 
df = df.set_index([0,1]) 
df.groupby([df.index.get_level_values(0),df.index.get_level_values(1)]).max() 

这使得从分组超过指数在0和1

+0

由于它的工作原理其他列的最大值! –