0

Sample of the dataframe

The three columns on the left are day month, and year. I am trying to fill in NaN values in the last column which I am calling 'C'. For each week of each month there is one non-empty value in the last column, and I would like to assign the NaN values with the non-empty value.

So far I have tried doing it with the first week with the following code:

for year in range(2013, 2023):
  for month in range(1, 13):
    for day in range(1, 8):
      df.loc[pd.isnull(df['C']), 'C'] = df.loc[(df['year'] == year) & (df['month'] == month) & (df['day'] == 3), 'C']

1 Answer 1

0

Build a column week and then use grouping over the columns year, month and week and use .ffill and .bfill:

df['week'] = df['day'].apply(lambda x: (x - 1) // 7 + 1)  # Assign week numbers
df['C'] = df.groupby(['year', 'month', 'week'])['C'].transform(lambda x: x.ffill().bfill())
Sign up to request clarification or add additional context in comments.

3 Comments

Why are you building the column week with .apply?
Applies the lambda function to each value in the day column. It calculates the week number for each day.
Yes, I know what it does, but you should avoid .apply whenever you can (it is a performance bottle neck). Why don't you just do df['week'] = (df['day'] - 1) // 7 + 1 or df['week'] = df['day'].add(-1).floordiv(7).add(1) instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.