import pandas as pd
# create dataframe and add column and rows to it
df_fruits = pd.DataFrame()
df_fruits["fruit_name"] = ["Orange", "Banana", "Apple", "Grapes"]
df_fruits["color"] = ["orange", "yellow", "red", "green"]
df_fruits["price"] = [50, 100, 150, 70]
# convert DataFrame to list of dictionaries
result = df_fruits.to_dict('records')
print(result)
Output
[
{'fruit_name': 'Orange', 'color': 'orange', 'price': 50},
{'fruit_name': 'Banana', 'color': 'yellow', 'price': 100},
{'fruit_name': 'Apple', 'color': 'red', 'price': 150},
{'fruit_name': 'Grapes', 'color': 'green', 'price': 70}
]
Our DataFrame will look like this:
╒════╤══════════════╤═════════╤═════════╕
│ │ fruit_name │ color │ price │
╞════╪══════════════╪═════════╪═════════╡
│ 0 │ Orange │ orange │ 50 │
├────┼──────────────┼─────────┼─────────┤
│ 1 │ Banana │ yellow │ 100 │
├────┼──────────────┼─────────┼─────────┤
│ 2 │ Apple │ red │ 150 │
├────┼──────────────┼─────────┼─────────┤
│ 3 │ Grapes │ green │ 70 │
╘════╧══════════════╧═════════╧═════════╛
You can pass 'records' as a parameter to the to_dict() method of the pandas DataFrame to convert a DataFrame to the list of dictionaries.
If you have a DataFrame and you want to convert it into a list of dictionaries, you can use the DataFrame.to_dict('records') function. This function will take your DataFrame and return a list of dictionaries, where each dictionary represents one row of the DataFrame.
Syntax
DataFrame.to_dict('records')
Code example
import pandas as pd
# create dataframe and add column and rows to it
df = pd.DataFrame({
'product_id': [1000, 1001, 1002, 1003, 1004],
'product_name': ['P01', 'P02', 'P03', 'P04', 'P05'],
'price': [330.0, 510.0, 23.0, 590.0, 620.4]
})
print(df)
# convert DataFrame to list of dictionaries
result = df.to_dict('records')
print(result)
Output
DataFrame output
╒════╤══════════════╤════════════════╤═════════╕
│ │ product_id │ product_name │ price │
╞════╪══════════════╪════════════════╪═════════╡
│ 0 │ 1000 │ P01 │ 330 │
├────┼──────────────┼────────────────┼─────────┤
│ 1 │ 1001 │ P02 │ 510 │
├────┼──────────────┼────────────────┼─────────┤
│ 2 │ 1002 │ P03 │ 23 │
├────┼──────────────┼────────────────┼─────────┤
│ 3 │ 1003 │ P04 │ 590 │
├────┼──────────────┼────────────────┼─────────┤
│ 4 │ 1004 │ P05 │ 620.4 │
╘════╧══════════════╧════════════════╧═════════╛
After converting to a list of dictionaries:
[
{'product_id': 1000, 'product_name': 'P01', 'price': 330.0},
{'product_id': 1001, 'product_name': 'P02', 'price': 510.0},
{'product_id': 1002, 'product_name': 'P03', 'price': 23.0},
{'product_id': 1003, 'product_name': 'P04', 'price': 590.0},
{'product_id': 1004, 'product_name': 'P05', 'price': 620.4}
]
The code example creates a DataFrame and then converts it to a list of dictionaries. The result variable contains the list of dictionaries.
The function DataFrame.T.to_dict().values() will output the same result as to_dict('records') but you need to use the list() function of python to convert the output to a list of dictionaries. Check the below code example for reference.
Syntax
list(DataFrame.T.to_dict().values())
Code example
import pandas as pd
# create dataframe and add column and rows to it
df = pd.DataFrame({
'product_id': [1000, 1001, 1002, 1003, 1004],
'product_name': ['P01', 'P02', 'P03', 'P04', 'P05'],
'price': [330.0, 510.0, 23.0, 590.0, 620.4]
})
# convert DataFrame to list of dictionaries
result = df.T.to_dict().values()
result = list(result)
print(result)
Output
[
{'product_id': 1000, 'product_name': 'P01', 'price': 330.0},
{'product_id': 1001, 'product_name': 'P02', 'price': 510.0},
{'product_id': 1002, 'product_name': 'P03', 'price': 23.0},
{'product_id': 1003, 'product_name': 'P04', 'price': 590.0},
{'product_id': 1004, 'product_name': 'P05', 'price': 620.4}
]
0 Comments