I have a simple problem which I've solved but it would be great if someone could explain why for loops do this in python and if there is a more elegant way. Really sorry if this is a dumb question - I have done my best to try multiple methods and look at related questions, but I still unsure why it doesn't work.
I've read this post, but it doesn't quite explain my issue: for loop only returning the last word in a list of many words
If I print x, it returns every month name perfectly.
monthName = []
for i in df["Month_Number"]:
x = calendar.month_abbr[i]
print(x)
The below stores the result (month names) it in a nice clean list (which is great).
monthName = []
for i in df["Month_Number"]:
x = calendar.month_abbr[i]
monthName.append(x)
I would then go on to solve my problem by doing this:
df["Month_Name"] = monthName
Why does the following ONLY return "NONE" when i integrate into the loop?
monthName = []
for i in df["Month_Number"]:
x = calendar.month_abbr[i]
df["Month_Name"] = monthName.append(x)
Why does the following only return the last value:
for i in df["Month_Number"]:
df["Month_Name"] = calendar.month_abbr[i]
I understand (to some degree) as to why append returns none, but was more interested in understanding why other approaches only return the LAST value.