Alberto F
Alberto F

Reputation: 95

Big pandas dataframe to dict of some columns

I have a big dataframe like:

       product   price    serial     category    department   origin    
 0     cookies       4      2345    breakfast          food        V
 1     paper       0.5      4556   stationery          work        V
 2     spoon         2      9843      kitchen     household        M

I want to convert to dict, but I just want an output like:

   {serial: 2345}{serial: 4556}{serial: 9843} and {origin: V}{origin: V}{origin: M}

where key is column name and value is value

Now, i've tried with df.to_dict('values') and I selected dic['origin'] and returns me

   {0: V}{1:V}{2:M}

I've tried too with df.to_dict('records') but it give me:

   {product: cookies, price: 4, serial:2345, category: breakfast, department:food, origin:V}

and I don't know how to select only 'origin' or 'serial'

Upvotes: 1

Views: 62

Answers (1)

Rajesh Bhat
Rajesh Bhat

Reputation: 1000

You can do something like:

serial_dict = df[['serial']].to_dict('r')
origin_dict = df[['origin']].to_dict('r')

Upvotes: 1

Related Questions