Reputation: 123
I want update the column counter to counter + 1 every time in encounters the string [hr]. My column named "Counter" would ideally look like 11111,22222,33333 .... 999999 s=
This is
My code is below
df['Counter'] = df['elapsed time'].apply(lambda x: 1 if x != "[hr]" else 2)
I try with the below but get the error SyntaxError: lambda cannot contain assignment
Is there a way around this?
df['Counter'] = df['elapsed time'].apply(lambda x: 1 if x != "[hr]" else x =+ 1)
Example dataframe
elapsed time distance travelled significant wave height total Counter
0 [hr] [nm] [m] 2
1 0.000 0.000 1.035 1
2 3.000 0.000 0.000 1
3 6.000 0.000 0.000 1
4 9.000 0.000 0.000 1
... ... ... ... ...
76774 320.638 4181.591 0.000 1
76775 321.000 4186.698 0.000 1
76776 322.116 4202.445 0.000 1
76777 323.181 4217.467 0.000 1
Upvotes: 1
Views: 121
Reputation: 863031
If want count next values after [hr]
value then compare values forst and then use cumulative sum:
df['Counter'] = df['elapsed time'].eq("[hr]").cumsum()
Upvotes: 1