Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop is only storing the last value in colum

Tags:

python

pandas

I am trying to pull the week number given a date and then add that week number to the corresponding row in a pandas/python dataframe.

When I run a for loop it is only storing the last calculated value instead of recording each value.

I've tried .append but haven't been able to get anything to work.

import datetime
from datetime import date

for i in df.index:
    
    week_number = date(df.year[i],df.month[i],df.day[i]).isocalendar()
    df['week'] = (week_number[1])

Expected values:

day month  year  week  
 8    12  2021    49  
19    12  2021    50  
26    12  2021    51  

Values I'm getting:
day month  year  week  
 8    12  2021    51  
19    12  2021    51  
26    12  2021    51  
like image 540
5tanczak Avatar asked Dec 26 '21 12:12

5tanczak


People also ask

Does for loop need a condition?

Generally, syntax of for loop is for(assign value, check condition, increment){} They have used the for loop but there is no condition checking, how does this work? because there is explicitly condition checking inside the for loop. If the condition gets false then it will return false.

What is a loop in R?

In R programming, we require a control structure to run a block of code multiple times. Loops come in the class of the most fundamental and strong programming concepts. A loop is a control statement that allows multiple executions of a statement or a set of statements. The word 'looping' means cycling or iterating.

Where are the current values of attendees stored in a loop?

Every time those loops run, the current values of attendees.get (i) and lesson_iDs.get (j) are stored in your two variables (user_id, lesson_id) removing the last stored value.

How to append the output of a for-loop to a vector?

First, we have to create an empty vector, in which we will store the output of our for-loop later on: Now, we can append the output of each for-loop iteration to this vector using the c () function in R:

Does RStudio create a data object from the last iteration of loop?

As you can see based on the following output of the RStudio console, we created a data object containing only the value created by the last iteration of the loop: Next, I’ll show how to fix this issue…


Video Answer


2 Answers

You can simply use Pandas .apply method to make it a one-liner:

df["week"] = df.apply(lambda x: date(x.year, x.month, x.day).isocalendar()[1], axis=1)
like image 54
Michael Boesl Avatar answered Oct 29 '22 01:10

Michael Boesl


You need to assign it back at the correct corresponding, i, position. Using loc, should help:

for i in df.index:
    week_number = date(df.year[i],df.month[i],df.day[i]).isocalendar()
    df.loc[i,'week'] = (week_number[1])

prints back:

print(df)

   day  month  year  week
0    8     12  2021    49
1   19     12  2021    50
2   26     12  2021    51
like image 37
sophocles Avatar answered Oct 29 '22 00:10

sophocles