python - how to convert csv to dictionary using pandas -
how can convert csv dictionary using pandas? example have 2 columns, , column1 key , column2 value. data looks this:
"name","position" "ucla","73" "suny","36" cols = ['name', 'position'] df = pd.read_csv(filename, names = cols)
convert columns list, zip , convert dict:
in [37]: df = pd.dataframe({'col1':['first','second','third'], 'col2':np.random.rand(3)}) print(df) dict(zip(list(df.col1), list(df.col2))) col1 col2 0 first 0.278247 1 second 0.459753 2 third 0.151873 [3 rows x 2 columns] out[37]: {'third': 0.15187291615699894, 'first': 0.27824681093923298, 'second': 0.4597530377539677}
Comments
Post a Comment