Home > Software design >  convert DF to dict
convert DF to dict

Time:09-17

I have a df and I want to convert it to a dict.

df looks like following.

Colleges     Location       Streams
JNTU         Hyd,Andhra      ECE,CSE,EE
OU           Hyd,Andhra      ECE,CSE,EE

I want the dict output like

{Colleges: JNTU , Location : Hyd,Andhra, Streams : ECE,CSE,EE}

Any inputs/ help is appreciated.

CodePudding user response:

Pandas to_dict method through you can convert DataFrame to dict

See the in-detail to_dict like this

df.to_dict('records')

Output will be

[
{"Colleges": "JNTU" , "Location" : "Hyd,Andhra", "Streams" : "ECE,CSE,EE"},
{"Colleges": "OU" , "Location" : "Hyd,Andhra", "Streams" : "ECE,CSE,EE"},
]
  • Related