Skip to content
Advertisement

How can I get a specific value from a pandas DataFrame?

I have a .df that looks something like this(df = pandas.read_csv(main_db)):

itemName itemBrand itemCode itemStock
some name some brand a 6 digit number some low number
even more names even more brands nore 6-digit numbers more stocks

Looks like that but with actual names and brands.

Now if I use result = df[df['itemCode']==itemCode], I get:

blank itemName itemBrand itemCode itemStock
6 itemname7 itembrand7 905616 13

It’s very good. I spend too damn long looking for this. Now, I’m looking to get only the itemStock (In this case 13) to use somewhere else. So here I use result2 = result['itemStock'].

6 13

Name: itemStock, dtype: int64

Hm. okay, not what I wanted. What can I do to get only 13?

Advertisement

Answer

If you use:

result2 = result['itemStock']

it returns you a data series so you see also the index and not only the value you want. You can check it using

type(result2)

You can find what you want using .values attribute

result2 = result['itemStock'].values
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement