python - What is the most efficient way to create a dictionary of two pandas Dataframe columns? - Stack Overflow

PHOTO EMBED

Wed Jan 06 2021 09:51:36 GMT+0000 (Coordinated Universal Time)

Saved by @arielvol #python

df = pd.DataFrame(np.random.randint(32, 120, 100000).reshape(50000,2),columns=list('AB'))
df['A'] = df['A'].apply(chr)

%timeit dict(zip(df.A,df.B))
%timeit pd.Series(df.A.values,index=df.B).to_dict()
%timeit df.set_index('A').to_dict()['B']
content_copyCOPY

https://stackoverflow.com/questions/17426292/what-is-the-most-efficient-way-to-create-a-dictionary-of-two-pandas-dataframe-co