I have the following DataFrame (this is a condensed version - it goes back a long time)
Week Commencing A1 A2 A3 A4
2016-01-03 28 1375 1999 1345
2016-01-10 48 1552 2428 1337
2016-01-17 43 1895 2615 1420
2016-01-24 29 1950 2568 1385
2016-01-31 41 1912 2577 1277
2016-02-07 29 2176 2771 1403
2016-02-14 50 2229 3013 1450
2016-02-21 60 2271 3029 1489
2016-02-28 43 2140 3133 1594
2016-03-06 51 2080 3140 1498
I want to create a new column that specifies a label based on a specific period in time. IE: if row is before a certain date, return a word.
I have tried the following:
def action(x):
if x == "True":
return "Before Migration"
if x == "False":
return "After Migration"
df.index.apply(action, axis=1)
I get the following error: "AttributeError: 'DatetimeIndex' object has no attribute 'apply'".
I have changed this to a string, tried resetting the index so I can apply to a column rather than index and it doesn't work.
I have also tried this:
if df.index < '2016-02-14':
df["Migration_Type"] = "Before Migration"
else:
df["Migration_Type"] = "After Migration"
Error: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Any suggestions on better ways are also appreciated.