简体   繁体   English

根据其他列中的信息从 Pandas Dataframe 中提取单个值

[英]Extracting a single value from a Pandas Dataframe based on info in other column

I have a dataframe that I would like to use basically as a look up table.我有一个数据框,我想基本上用作查找表。 If there is a better option than a dataframe, I'm open to that option, too.如果有比数据框更好的选择,我也愿意接受该选择。 Original data is sitting in an Excel spreadsheet.原始数据位于 Excel 电子表格中。

Here a short version of the dataframe:这是数据框的简短版本:

Content = [["Trees", "units / kg", 0.015333728],
          ["Fertiliser", "kg / kg", 0.33942757],
         ["Pesticide packaging", "kg / kg", 0.031279937],
         ["Jute bag", "kg / kg", 0.00025]]
Column_Titles = ["Name", "Unit", "Value"]

df = pd.DataFrame(Content,columns=Column_Titles)

I now want to search in "Name" for eg "Jute Bag" and extract the corresponding value (0.00025 in this case) and only the value.我现在想在“名称”中搜索例如“黄麻袋”并提取相应的值(在本例中为 0.00025)和仅提取值。

The closest I have come so far is this:到目前为止,我最接近的是:

test = Constants.loc[Constants['Name'] == 'Jute bag', 'Value']

but this gives me但这给了我

3    0.00025
Name: Value, dtype: float64

How do I now get only the 0.00025 or is there overall a better way to do this?我现在如何只获得 0.00025 或者总体上是否有更好的方法来做到这一点? Thanks!谢谢!

或者你可以做

test = Constants['Value'].loc[Constants['Name'] == 'Jute bag'].values[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM