user01
user01

Reputation: 13

Iteration to create new list

day_of_week = []
for i in range(len(data2)):
    if data2["funded date"][i][0:3] == "Sun":
        day_of_week[i] = 1
    elif data2["funded date"][i][0:3] == "Mon":
        day_of_week[i] = 2
    elif data2["funded date"][i][0:3] == "Tue":
        day_of_week[i] = 3
    elif data2["funded date"][i][0:3] == "Wed":
        day_of_week[i] = 4
    elif data2["funded date"][i][0:3] == "Thu":
        day_of_week[i] = 5
    elif data2["funded date"][i][0:3] == "Fri":
        day_of_week[i] = 6
    else:
        day_of_week[i] = 7

print(day_of_week)

Cannot determine how to create a new list that will give an integer value for the day of the week.

Upvotes: 0

Views: 54

Answers (1)

Mike67
Mike67

Reputation: 11342

You can create a dictionary of days then use list comprehension to create the day list

days = {d:i+1 for i,d in enumerate(['Sun','Mon','Tue','Wed','Thu','Fri','Sat'])}

day_of_week = [days[data2["funded date"][i][0:3]] for i in range(len(data2))]

Upvotes: 1

Related Questions