Sep-09-2022, 07:27 PM
(This post was last modified: Sep-09-2022, 07:56 PM by deanhystad.)
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