Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compound if else statement
#9
import pandas as pd
import random
from time import time

states = ["Morning", "Afternoon", "Evening", "Night"]
df = pd.DataFrame({"state": random.choices(states, k=10000000)})

a = time()
for i, state in enumerate(states, start=1):
    df.loc[df["state"] == state, ["state"]] = i

print(time() - a)
Output:
1.6639153957366943
Almost twice as fast as calling a functon/lambda. Nowhere near as fast as passing a dictionary
Reply


Messages In This Thread
Compound if else statement - by Led_Zeppelin - Sep-08-2022, 12:42 PM
RE: Compound if else statement - by deanhystad - Sep-08-2022, 02:02 PM
RE: Compound if else statement - by Led_Zeppelin - Sep-08-2022, 02:50 PM
RE: Compound if else statement - by deanhystad - Sep-08-2022, 05:02 PM
RE: Compound if else statement - by Led_Zeppelin - Sep-08-2022, 05:12 PM
RE: Compound if else statement - by deanhystad - Sep-08-2022, 06:09 PM
RE: Compound if else statement - by deanhystad - Sep-08-2022, 07:19 PM
RE: Compound if else statement - by perfringo - Sep-09-2022, 08:16 AM
RE: Compound if else statement - by deanhystad - Sep-09-2022, 07:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Understanding compound operators -= NewatCode 3 3,237 Apr-25-2018, 05:03 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020