Home > Mobile >  Extracting a single value from a Pandas Dataframe based on info in other column
Extracting a single value from a Pandas Dataframe based on info in other column

Time:10-16

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 e.g. "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!

CodePudding user response:

Alternatively you can do

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