1

I am currently trying to retrieve values from cells in a Dataframe. I am searching through a dataframe to find strings that match values in a column in a row, then returning a value in that row from another column.

My code looks like this:

df:
Fruit   Value
apple     7.0
banana    6.0
orange    8.0
lemon     3.0
melon     2.0

myList = ['apple', 'lemon']

result = []

for word in myList:

    result.append(df['Value'].loc[df['Fruit'] == word].values)

print(result)

The print statement is outputting:

[array([], dtype=float64), array([7.0]), array([], dtype=float64), array([3.0])]

My desired output is simply an array of float values:

[7.0, 3.0]

How would I go about cleaning my output to achieve this? My end goal is to get the average value of the array.

Kishan Patel
  • 778
  • 11
  • 26
Learning Developer
  • 156
  • 2
  • 4
  • 14

1 Answers1

1

Use Series.isin with boolean indexing and convert to list:

print (df.loc[df['Fruit'].isin(myList), 'Value'].values.tolist())
[7.0, 3.0]

My end goal is to get the average value of the array.

Then better is call mean of filtered Series:

val = df.loc[df['Fruit'].isin(myList), 'Value'].mean()
print (val)
5.0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252