May-30-2019, 01:53 PM
I need to define a function that will calculate and return out of dataframe that includes columns "Height" and "Weight", the BMI of each row.
Here is the code that i wrote:
the result is an error:
Here is the code that i wrote:
def BMI(DataFrame): df=pd.DataFrame return 'Weight'/'Height'**2But when I try to run that function with:
data.apply(BMI,axis=1)when "data" is my dataframe,
the result is an error:
Error:Traceback (most recent call last):
File "<input>", line 1, in <module>
File "D:\Python Projects\venv\lib\site-packages\pandas\core\frame.py", line 6487, in apply
return op.get_result()
File "D:\Python Projects\venv\lib\site-packages\pandas\core\apply.py", line 151, in get_result
return self.apply_standard()
File "D:\Python Projects\venv\lib\site-packages\pandas\core\apply.py", line 257, in apply_standard
self.apply_series_generator()
File "D:\Python Projects\venv\lib\site-packages\pandas\core\apply.py", line 286, in apply_series_generator
results[i] = self.f(v)
File "<input>", line 3, in BMI
TypeError: ("'type' object is not subscriptable", 'occurred at index 0')
What went wrong with my code?