How to convert rows in DataFrame in Python to dictionaries

Vicky

For example, I have DataFrame now as

id score1   score2  score3  score4  score5
1  0.000000     0.108659    0.000000    0.078597    1
2  0.053238     0.308253    0.286353    0.446433    1
3  0.000000     0.083979    0.808983    0.233052    1

I want to convert it as

id scoreDict
1  {'1': 0, '2': 0.1086, ...}
2  {...}
3  {...}

Anyway to do that?

Jianxun Li
import pandas as pd

# your df
# =========================
print(df)

   id  score1  score2  score3  score4  score5
0   1  0.0000  0.1087  0.0000  0.0786       1
1   2  0.0532  0.3083  0.2864  0.4464       1
2   3  0.0000  0.0840  0.8090  0.2331       1

# to_dict
# =========================
df.to_dict(orient='records')

Out[318]: 
[{'id': 1.0,
  'score1': 0.0,
  'score2': 0.10865899999999999,
  'score3': 0.0,
  'score4': 0.078597,
  'score5': 1.0},
 {'id': 2.0,
  'score1': 0.053238000000000001,
  'score2': 0.308253,
  'score3': 0.28635300000000002,
  'score4': 0.44643299999999997,
  'score5': 1.0},
 {'id': 3.0,
  'score1': 0.0,
  'score2': 0.083978999999999998,
  'score3': 0.80898300000000001,
  'score4': 0.23305200000000001,
  'score5': 1.0}]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to convert a dict to a dataframe, with the values as headers and rows?

From Java

Convert DataFrame to dictionary with sub-dictionaries

From Java

Convert list of dictionaries to a pandas DataFrame

From Java

How to convert DataFrame column to Rows in Python?

From Java

Convert dictionary of dictionaries to dataframe with data types

From Java

Convert dictionaries with list of values into a dataframe

From Dev

JSON dictionaries to Pandas DataFrame in Python

From Dev

Convert a list of dictionaries with varying keys to a dataframe

From Dev

How to select rows in a DataFrame based on rows in another DataFrame using Python

From Dev

How convert list of dictionaries to a dictionary of dictionaries in Python

From Dev

How to convert the dataframe to array in python?

From Dev

How to convert a list of tuples to a dictionary of dictionaries in Python?

From Dev

How to convert a list of dictionaries to JSON in Python / Django?

From Dev

Convert pandas.DataFrame to list of dictionaries in Python

From Dev

Convert dictionaries with list of values into a dataframe

From Dev

How to convert list of dictionaries into Pyspark DataFrame

From Dev

Convert list into dataframe python by converting list elements into rows and columns

From Dev

Convert Nested dictionaries to tuples python

From Dev

How to convert a string to variable in python without using dictionaries?

From Dev

Convert list of dictionaries to EXCEL with Python

From Dev

How to quickly convert from items in a list of lists to list of dictionaries in python?

From Dev

JSON dictionaries to Pandas DataFrame in Python

From Dev

How to convert lists in a dictionary to dictionaries in a lists (Python)

From Dev

How convert list of dictionaries to a dictionary of dictionaries in Python

From Dev

How to create dictionary of dictionaries from a dataframe using python's pandas

From Dev

How to convert python JSON rows to dataframe columns without looping

From Dev

Convert a pandas dataframe into a dictionary of dictionaries

From Dev

How to convert a triplet DataFrame to a new DataFrame with no duplicate rows?

From Dev

How to convert string containing list of dictionaries to python (object) LIST of DICT

Related Related

  1. 1

    How to convert a dict to a dataframe, with the values as headers and rows?

  2. 2

    Convert DataFrame to dictionary with sub-dictionaries

  3. 3

    Convert list of dictionaries to a pandas DataFrame

  4. 4

    How to convert DataFrame column to Rows in Python?

  5. 5

    Convert dictionary of dictionaries to dataframe with data types

  6. 6

    Convert dictionaries with list of values into a dataframe

  7. 7

    JSON dictionaries to Pandas DataFrame in Python

  8. 8

    Convert a list of dictionaries with varying keys to a dataframe

  9. 9

    How to select rows in a DataFrame based on rows in another DataFrame using Python

  10. 10

    How convert list of dictionaries to a dictionary of dictionaries in Python

  11. 11

    How to convert the dataframe to array in python?

  12. 12

    How to convert a list of tuples to a dictionary of dictionaries in Python?

  13. 13

    How to convert a list of dictionaries to JSON in Python / Django?

  14. 14

    Convert pandas.DataFrame to list of dictionaries in Python

  15. 15

    Convert dictionaries with list of values into a dataframe

  16. 16

    How to convert list of dictionaries into Pyspark DataFrame

  17. 17

    Convert list into dataframe python by converting list elements into rows and columns

  18. 18

    Convert Nested dictionaries to tuples python

  19. 19

    How to convert a string to variable in python without using dictionaries?

  20. 20

    Convert list of dictionaries to EXCEL with Python

  21. 21

    How to quickly convert from items in a list of lists to list of dictionaries in python?

  22. 22

    JSON dictionaries to Pandas DataFrame in Python

  23. 23

    How to convert lists in a dictionary to dictionaries in a lists (Python)

  24. 24

    How convert list of dictionaries to a dictionary of dictionaries in Python

  25. 25

    How to create dictionary of dictionaries from a dataframe using python's pandas

  26. 26

    How to convert python JSON rows to dataframe columns without looping

  27. 27

    Convert a pandas dataframe into a dictionary of dictionaries

  28. 28

    How to convert a triplet DataFrame to a new DataFrame with no duplicate rows?

  29. 29

    How to convert string containing list of dictionaries to python (object) LIST of DICT

HotTag

Archive