export dataframe to dict code example

Example 1: dataframe to dictionary with one column as key

pd.Series(df.A.values,index=df.B).to_dict()

Example 2: df col to dict

area_dict = dict(zip(lakes.area, lakes.count))

Example 3: pandas to dictionary

df.to_dict('records')

Example 4: dataframe to dict without index

{0: {'Name': 'John', 'Age': 38, 'City': 'Boston'},
 1: {'Name': 'Sara', 'Age': 47, 'City': 'Charlotte'},
 2: {'Name': 'Peter', 'Age': 63, 'City': 'London'},
 3: {'Name': 'Cecilia', 'Age': 28, 'City': 'Memphis'}}

Tags:

Java Example