Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Apply function on different columns as defined DavidGG Unladen Swallow Posts: 4 Threads: 2 Joined: May 2019 Reputation: 0 Likes received: 0 #1 Jun-02-2019, 08:01 AM (This post was last modified: Jun-02-2019, 08:01 AM by DavidGG. Edited 1 time in total.) I have a function that returns the BMI from a given dataframe with columns 'Weight' and 'Height' Here is the function: ```def BMI(data): return data['Weight']/(data['Height']**2)```Now, i added new column 'Height In Meters' to the dataframe 'data' with: `data['Height In Meters']=data['Height']/100`What i would like to do next, is to apply the original function on the dataframe 'data', but instead of using the column 'Height', the calculation would be by using the new column 'Height In Meters'. the result should be a new column called 'BMI' in the dataframe 'data', that shows for each row the calculation using 'Height In Meters'. I tried: `data['BMI']=data[['Weight','Height In Meters']].apply(BMI,axis=1)`But that doesn't seem to work. ichabod801 Bunny Rabbit Posts: 3,526 Threads: 73 Joined: Sep 2016 Reputation: 197 Likes received: 1072 #2 Jun-02-2019, 10:30 AM try: ```data['BMI'] = data[['Weight', 'Height In Meters']].rename(columns = ['Weight', 'Height']).apply(BMI, axis = 1) ``` Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures DavidGG Unladen Swallow Posts: 4 Threads: 2 Joined: May 2019 Reputation: 0 Likes received: 0 #3 Jun-02-2019, 12:05 PM (Jun-02-2019, 10:30 AM)ichabod801 Wrote: try: ```data['BMI'] = data[['Weight', 'Height In Meters']].rename(columns = ['Weight', 'Height']).apply(BMI, axis = 1) ``` Thank you for trying to help. I still get an unclear error message when running the code: ``````Error:Traceback (most recent call last): File "", line 1, in File "D:\Python Projects\venv\lib\site-packages\pandas\util\_decorators.py", line 197, in wrapper return func(*args, **kwargs) File "D:\Python Projects\venv\lib\site-packages\pandas\core\frame.py", line 4025, in rename return super(DataFrame, self).rename(**kwargs) File "D:\Python Projects\venv\lib\site-packages\pandas\core\generic.py", line 1091, in rename level=level) File "D:\Python Projects\venv\lib\site-packages\pandas\core\internals\managers.py", line 171, in rename_axis obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level)) File "D:\Python Projects\venv\lib\site-packages\pandas\core\internals\managers.py", line 2004, in _transform_index items = [func(x) for x in index] File "D:\Python Projects\venv\lib\site-packages\pandas\core\internals\managers.py", line 2004, in items = [func(x) for x in index] TypeError: 'list' object is not callable`````` ichabod801 Bunny Rabbit Posts: 3,526 Threads: 73 Joined: Sep 2016 Reputation: 197 Likes received: 1072 #4 Jun-02-2019, 10:05 PM Do you have two things named BMI? That error makes it look like you named a list the same thing as your function. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures DavidGG Unladen Swallow Posts: 4 Threads: 2 Joined: May 2019 Reputation: 0 Likes received: 0 #5 Jun-03-2019, 06:25 AM (Jun-02-2019, 10:05 PM)ichabod801 Wrote: Do you have two things named BMI? That error makes it look like you named a list the same thing as your function. My function is called BMI, and the column i would like to add to the dataframe also called 'BMI'. Any other suggestions? ichabod801 Bunny Rabbit Posts: 3,526 Threads: 73 Joined: Sep 2016 Reputation: 197 Likes received: 1072 #6 Jun-03-2019, 01:12 PM Try renaming your function. At the time that line executes, BMI is a list. Maybe that's getting done in the pandas code somewhere. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post how to apply user defined function to Pandas DataFrame evelynow 3 252 Aug-20-2019, 11:35 PM Last Post: scidam Creating A List of DataFrames & Manipulating Columns in Each DataFrame firebird 1 112 Jul-31-2019, 04:04 AM Last Post: scidam compound columns using IN rregorr 3 364 Jul-22-2019, 11:27 PM Last Post: scidam display graph in columns and rows william888 1 227 Jul-02-2019, 10:19 AM Last Post: dataman [pandas] How to re-arrange DataFrame columns SriMekala 8 696 Jun-22-2019, 12:55 AM Last Post: scidam Distance between 2 user defined geo-grids in km chandrakant98c 0 446 Jun-16-2019, 11:26 AM Last Post: chandrakant98c The combination of four columns in two. pawlo392 2 293 May-31-2019, 01:54 PM Last Post: heiner55 Pandas - cumulative sum of two columns tobbs 12 676 May-25-2019, 08:37 PM Last Post: tobbs Selecting Few Columns from a dataframe Shivi_Bhatia 2 330 Mar-24-2019, 12:20 PM Last Post: Shivi_Bhatia compare and modify columns in dataframe DionisiO 1 293 Feb-23-2019, 11:07 PM Last Post: tiredAcademic

Forum Jump:

Users browsing this thread: 1 Guest(s)