(May-30-2019, 02:34 PM)heiner55 Wrote:#!/usr/bin/python3 import pandas as pd data = [ { 'h': 1.80, 'w': 80 }, { 'h': 1.70, 'w': 90 }, { 'h': 1.60, 'w': 60 }, ] df = pd.DataFrame(data) print(df) print("------------------") def BMI(data): return data['w'] / data['h']**2 df['bmi'] = df.apply(BMI, axis=1) print(df)
Output:h w 0 1.8 80 1 1.7 90 2 1.6 60 ------------------ h w bmi 0 1.8 80 24.691358 1 1.7 90 31.141869 2 1.6 60 23.437500
There are better ways with pandas - you can operate on rows/columns directly
df['bmi'] = df.w / df.h ** 2
Output: h w bmi
0 1.8 80 24.691358
1 1.7 90 31.141869
2 1.6 60 23.437500
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.