Pandas - 最低费率和相应的供应商名称

问题描述 投票:0回答:1

[我目前正在分析一些价格数据,想知道是否有可能在 pandas dataframe 中获得每个通道 id 的最低费率以及提供最便宜费率的供应商的相应名称]

enter image description here

Below is my expected output. df[['quoted_rates']].idxmin() but it is not giving all the information that I would like to have. Please help me. Appreciate the support, thank you

python pandas dataframe group-by minimum
1个回答
0
投票
data = [['Supplier 1', 'Lane 1', 'NL', 'DE', 200],
        ['Supplier 2', 'Lane 1', 'NL', 'DE', 150],
        ['Supplier 3', 'Lane 1', 'NL', 'DE', 300],
        ['Supplier 1', 'Lane 2', 'NL', 'DE', 200],
        ['Supplier 2', 'Lane 2', 'NL', 'DE', 105],
        ['Supplier 3', 'Lane 2', 'NL', 'DE', 100]]

columns = ['supplier_name', 'lane_id', 'origin', 'destination', 'quoted_rates']

df = pd.DataFrame(data, columns=columns)
df.sort_values(by='quoted_rates').groupby('lane_id',as_index=False).first()

输出:

    lane_id     supplier_name   origin  destination     quoted_rates
0   Lane 1      Supplier 2      NL      DE              150
1   Lane 2      Supplier 3      NL      DE              100
© www.soinside.com 2019 - 2024. All rights reserved.