首页 > 解决方案 > 如何从数据框中的两列的除法中找到最小值

问题描述

我想找到两列的最小除法,只有列表中第三列的值。我的数据框是:

   ID  size  price
0   1     5    300
1   2    10    500
2   3    20    600
3   4    35    800
4   5    65    900
5   6    70   1000 

我想找到最低价格/尺寸,只能从列表中具有值的 id 中找到。

ids_wanted = [1,4,6]

我做了这段代码,它可以工作,但我觉得为该任务创建一个新的数据框既昂贵又没有必要。

import numpy as np
import pandas as pd
index = [0,1,2,3,4,5]
i = pd.Series([1,2,3,4,5,6], index=index)
s = pd.Series([5,10,20,35,65,70],index= index)
p = pd.Series([300,500,600,800,900,1000],index= index)
df = pd.DataFrame(np.c_[i,s,p],columns = ["ID","size","price"])
print("original df:\n",df,"\n")

ids_wanted = [1,4,6]
df_with_ids_wanted = df.loc[df['ID'].isin(ids_wanted)]
print("df with ids wanted:\n",df_with_ids_wanted,"\n")
price_per_byte = df_with_ids_wanted['price'] / df_with_ids_wanted['size']
df_with_ids_wanted_ppb = df_with_ids_wanted.assign(pricePerByte=price_per_byte)
print("df with ids wanted and price/size column:\n",df_with_ids_wanted_pps,"\n")
min_ppb = df_with_ids_wanted_pps['pricePerByte'].min()
print("min price per byte:",min_ppb)

输出:

original df:
    ID  size  price
0   1     5    300
1   2    10    500
2   3    20    600
3   4    35    800
4   5    65    900
5   6    70   1000 

df with ids wanted:
    ID  size  price
0   1     5    300
3   4    35    800
5   6    70   1000 

df with ids wanted and price/size column:
    ID  size  price  pricePerByte
0   1     5    300     60.000000
3   4    35    800     22.857143
5   6    70   1000     14.285714 

min price per byte: 14.285714285714286

标签: pythondataframe

解决方案


如果你想简洁,你可以试试这个:

i = range(1,7)
s = [5,10,20,35,65,70]
p = [300,500,600,800,900,1000]
df = pd.DataFrame({"ID":i,"size":s,"price":p})
df

输出:

    ID  size    price
0   1   5   300
1   2   10  500
2   3   20  600
3   4   35  800
4   5   65  900
5   6   70  1000

下一行将如下所示:

id_chosen = [1,4,6]
(df[df.ID.isin(id_chosen)]["price"]/df[df.ID.isin(id_chosen)]["size"]).min()

输出:

14.285714285714286

或者

min_div = (df[df.ID.isin(id_chosen)]["price"]/df[df.ID.isin(id_chosen)]["size"]).min()
print("the minimum price/size is {}".format(min_div))

输出:

the minimum price/size is 14.285714285714286

这样,您不必创建新的数据框。希望这可以帮助。


推荐阅读