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())
3
  • Why are you building the column week with .apply?
    – Timus
    Commented Mar 17 at 10:51
  • Applies the lambda function to each value in the day column. It calculates the week number for each day. Commented Mar 18 at 7:07
  • 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?
    – Timus
    Commented Mar 18 at 8:20

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.