Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Complete list assigned to each row in python

I created a list as a mean of 2 other columns, the length of the list is same as the number of rows in the dataframe. But when I try to add that list as a column to the dataframe, the entire list gets assigned to each row instead of only corresponding values of the list.

glucose_mean = []
for i in range(len(df)):
    mean = (df['h1_glucose_max']+df['h1_glucose_min'])/2
    glucose_mean.append(mean)

df['glucose'] = glucose_mean

data after adding list

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think you overcomplicated it. You don’t need for-loop but only one line

df['glucose'] = (df['h1_glucose_max'] + df['h1_glucose_min']) / 2

EDIT:

If you want to work with every row separatelly then you can use .apply()

def func(row):
   return (row['h1_glucose_max'] + row['h1_glucose_min']) / 2

df['glucose'] = df.appy(func, axis=1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading