I am working my way through an exercise using pandas. The Pandas data frame has a few columns, named as What_Type, Spend, Profit and corresponding values on the rows
A user defined function has been written to interrogate the first column "what_type" and return a value based on the value in the data frame. I believe the code is saying, if any of the values, hotels, fishing, Soccer are in the what_type column then apply some formula such as 1.5 * 10.
My question is in regards to code and how this function would be called to return a value.
(please see example below)
import pandas as pd
data = [['hotels', 10],['fishing', 20],['soccer', 15]]
df = pd.DataFrame(data, columns =['what_type', 'spend'])
def what_type(x):
if ['what_type'] in ['hotels', 'fishing', 'soccer']:
what_type = 1.5 * 10
else:
what_type = 2.1 * 5
return what_type
If I wanted to return the what_type value to my screen for the value 'hotel' (1.5 * 10) = 15, would I use the code below to return that value?
df.apply(what_type, axis = 1)
The result i get is this....
0 10.5
1 10.5
2 10.5
dtype: float64
I was expecting to get the output value 15.
thank you in advance :)