python - 如何从数据框中的两列的除法中找到最小值
问题描述
我想找到两列的最小除法,只有列表中第三列的值。我的数据框是:
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
解决方案
如果你想简洁,你可以试试这个:
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
这样,您不必创建新的数据框。希望这可以帮助。
推荐阅读
- task - FreeRTOS - vTaskList 未定义参考
- caching - 缓存 JPA 原生查询
- html - HTML5 视频无法播放/自动播放
- wordpress - Wordpress 自定义重写
- c# - 不与黑客共享连接字符串的最佳方法
- matlab - 在 matlab 中编译 MatConvnet,得到 nvcc 错误:'cicc' 死亡,状态为 0xC0000005 (ACCESS_VIOLATION)
- python - mypy:“__eq__”与超类型“object”不兼容
- pine-script - 如何在过去 n 天内进行 SMA 交叉?
- typescript - TypeScript 中的类型安全谓词函数
- windows - CS_DBLCLKS 有什么缺点吗?