简体   繁体   中英

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.

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.

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? Thanks!

或者你可以做

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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