【问题标题】:delete specific row in python pandas DataFrame based on row comparison根据行比较删除python pandas DataFrame中的特定行
【发布时间】:2017-06-19 00:32:45
【问题描述】:

我是编程和 python 的新手。我有一个由 pandas 模块创建的 DataFrame。数据集的索引列是“Rho”、“Capacity”和“Model-Version”。基于这些因素,我进行了一些模拟,并找到了每个运行 5 次的收入。这些列分别表示“平均值”、“下限”和“上限”。

现在我想为每个特定的 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)

但是,我知道这行不通。有人知道我该怎么做吗?

谢谢

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    您可能应该只找到最大收入。

    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 分组的其他列的最大值。

    【讨论】:

      猜你喜欢
      • 2012-09-14
      • 2022-06-15
      • 2018-04-28
      • 2016-05-05
      • 2013-08-12
      • 2021-02-27
      • 2018-01-11
      • 2017-02-16
      • 2020-11-26
      相关资源
      最近更新 更多