-2

I'm working with pandas. I have an existing column with accumulated days starting in the second day, ex -> [2, 3, 4, 5, 6, 7, 8, 9, 10, ..]. I'm trying to create a new Dataframe with the count of the week. I want to use lambda for this but I'm not able to figure out how to do this. The code looks like this

day_counter = 1
week = 1
df_eqpt['WeekNumber'] = df_eqpt['day'].apply(lambda x: week + 1 if day_counter == day_counter + 7 else day_counter += 1)

Any suggestions of how to go about this are much appreciated :)

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Welcome to Stack Overflow! Check out the [tour]. This seems to be an [XY problem](https://meta.stackexchange.com/q/66377/343832). That is, the question in the title is tangential to the problem you're actually trying to solve, and I'm pretty sure there's a better way to do it, like using slicing for example. You might want to [edit] and make the title about the actual problem. It'd also help to provide a [mre] including a sample of `df_eqpt` and your desired output. For specifics, see [How to make good reproducible pandas examples](/q/20109391/4518341). BTW, for overall tips, check out [ask]. – wjandrea Apr 24 '22 at 02:08
  • Can you add an input dataframe and excepted results? – Scott Boston Apr 24 '22 at 02:23
  • Can you provide an example of the expected output? – keramat Apr 24 '22 at 02:25
  • I think I figured out a way to do it using floor division, but I want to make sure I understand what you want first. – wjandrea Apr 24 '22 at 03:36
  • The root of your problem is `day_counter == day_counter + 7` which is never going to be true. Did you mean `day_counter == 7`?? – Frank Yellin Apr 24 '22 at 06:09

1 Answers1

0
  1. You say you want to implement with a lambda, but it's not working. Keep it simple and self explanatory: def a named function rather than writing an anonymous lambda.

  2. Scope of local variables within a function isn't doing you any favors. You might choose to use the global keyword, but it's probably better to create a new class Counter: and maintain state in self.day_count and self.week.

  3. You might find it simpler to define start as a convenient type such as datetime, and then increment a timedelta.

  4. Consider tacking on an extra column that has a sequential integer, or maybe the df index already offers that. Then, instead of needing .apply( ... ), you could do a much simpler assignment of an expression.

  5. Perhaps the .isocalendar() function would prove handy.

J_H
  • 17,926
  • 4
  • 24
  • 44