The problem of converting a dictionary to a DataFrame Note that the default dictionary is '1': [1,2,3], not '1': 1. That is, the dictionary will be converted to a DataFrame in the form of a list later.

Original
pandas convert dictionary to DataFrame

Convert dict d = {'A':0}to DataFrame,
first of all, the syntax format of DataFrame should be:

import pandas as pd
df = pd.DataFrame({'A':[0]})#'A'是columns,对应的是list
  • 1
  • 2

output:

   A
0  0
  • 1
  • 2

But if it is:

df = pd.DataFrame({'A':0})#直接输入dict
  • 1

will report an error

ValueError: If using all scalar values, you must pass an index
  • 1

Solution 1:
Specify index

pd.DataFrame({'A':0},index=[0])
  • 1

Solution 2:
Put the dictionary in a list

pd.DataFrame([{'A':0},{'A':1}])
  • 1

output:

   A
0  0
1  1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337907&siteId=291194637