Convert Python Dict into a Dataframe

Convert Python dict into a dataframe

The error here, is since calling the DataFrame constructor with scalar values (where it expects values to be a list/dict/... i.e. have multiple columns):

pd.DataFrame(d)
ValueError: If using all scalar values, you must must pass an index

You could take the items from the dictionary (i.e. the key-value pairs):

In [11]: pd.DataFrame(d.items())  # or list(d.items()) in python 3
Out[11]:
0 1
0 2012-07-02 392
1 2012-07-06 392
2 2012-06-29 391
3 2012-06-28 391
...

In [12]: pd.DataFrame(d.items(), columns=['Date', 'DateValue'])
Out[12]:
Date DateValue
0 2012-07-02 392
1 2012-07-06 392
2 2012-06-29 391

But I think it makes more sense to pass the Series constructor:

In [21]: s = pd.Series(d, name='DateValue')
Out[21]:
2012-06-08 388
2012-06-09 388
2012-06-10 388

In [22]: s.index.name = 'Date'

In [23]: s.reset_index()
Out[23]:
Date DateValue
0 2012-06-08 388
1 2012-06-09 388
2 2012-06-10 388

how to convert a python dict to a pandas dataframe

You can create dataframe directly, using dictionary:

my_dict2 = {"c": {"x": "aa", "y": "bb"}, "a": 1, "b": [1, 2, 3]}
df = pd.DataFrame(
{"prop_name": my_dict2.keys(), "prop_value": my_dict2.values()}
)
print(df)

Prints:

  prop_name              prop_value
0 c {'x': 'aa', 'y': 'bb'}
1 a 1
2 b [1, 2, 3]

With my_dict2 = {"a": 1, "b": [1, 2, 3], "c": {"x": "aa", "y": "bb"}} this produces:

  prop_name              prop_value
0 a 1
1 b [1, 2, 3]
2 c {'x': 'aa', 'y': 'bb'}

Note: as @TrentonMcKinney said in the comments, the behaviour of how the dataframe is constructed depends on the first item of the dictionary (source):

                if isinstance(list(data.values())[0], (Series, dict)):
data = _from_nested_dict(data)
else:
data, index = list(data.values()), list(data.keys())

so pd.DataFrame.from_dict({"b": 1, "a": [1, 2, 3]}, orient="index") succeeds and pd.DataFrame.from_dict({"a": [1, 2, 3], "b": 1},orient="index") produces an error.

converting dictionary to pandas dataframe in python

I may be missing something here but is just this what you're after?

df2 = pd.DataFrame([invoice_header])

Looks the same as df to me

How to convert python dictionary to pandas dataframe

Just get rid of those lists and you can feed directly to the DataFrame constructor:

pd.DataFrame({k: v[0] for k,v in my_dict.items()}).T

output:

         columns_1  columns_2
table_1 148989 437643
table_2 3344343 9897833

With the index as column:

(pd.DataFrame({k: v[0] for k,v in my_dict.items()})
.T
.rename_axis('table_name')
.reset_index()
)

output:

  table_name  columns_1  columns_2
0 table_1 148989 437643
1 table_2 3344343 9897833

Converting dictionary into a pandas dataframe

If you using all scalar values, you must pass an index

df = pd.DataFrame({'age': '34', 'max_heart_rate': '123', 'rest_blood_pressure': '132',
'blood_sugar': '1', 'exercice_angina': '0', 'chest_pain_type': 'typ_angina',
'rest_electro': 'normal', 'id': 8808},index = [0])


age max_heart_rate rest_blood_pressure blood_sugar exercice_angina chest_pain_type rest_electro id
0 34 123 132 1 0 typ_angina normal 8808

Convert Dictionary into Dataframe

Try to put the dictionary inside list ([]):

import pandas as pd

dct = {"A": [1, 2, 3], "B": [1, 2, 3, 4]}

df = pd.DataFrame([dct])
print(df)

Prints:

           A             B
0 [1, 2, 3] [1, 2, 3, 4]

Note: Don't use reserved words such as dict for variable names.

Convert dict to pandas dataframe?

You can use json_normalize, but repeated values are in last column(s):

df = pd.json_normalize(d, 'dp', 'id')
print(df)
Value Key id
0 11 abc 666
1 88 kuku 666
2 99 lulu 666
3 John name 666

For correct ordering use:

#create list of columns dynamic - all columns names without dp
cols = [c for c in d.keys() if c != 'dp']
print(cols)
['id']

df = pd.json_normalize(d, 'dp', 'id')
#change ordering by joined lists
df = df[cols + df.columns.difference(cols, sort=False).tolist()]
print(df)
id Value Key
0 666 11 abc
1 666 88 kuku
2 666 99 lulu
3 666 John name

Convert Python 3D dict into a dataframe

Use concat with DataFrame.reset_index:

return pd.concat(pd.read_excel(xl,sheet_name=sheets,index_col=False)).reset_index(drop=True)


d = {'SheetName':  pd.DataFrame({'Col1':[27,25], 'Col2':list('ab')}),
'SheetName1': pd.DataFrame({'Col1':[270,205], 'Col2':list('th')})}
print (d)
{'SheetName': Col1 Col2
0 27 a
1 25 b, 'SheetName1': Col1 Col2
0 270 t
1 205 h}

By default get MultiIndex:

df = pd.concat(d)
print (df)
Col1 Col2
SheetName 0 27 a
1 25 b
SheetName1 0 270 t
1 205 h

For convert it to column use:

df = pd.concat(d).droplevel(1).rename_axis('sheets').reset_index()
print (df)
sheets Col1 Col2
0 SheetName 27 a
1 SheetName 25 b
2 SheetName1 270 t
3 SheetName1 205 h

For remove MultiIndex use:

df = pd.concat(d).reset_index(drop=True)
print (df)
Col1 Col2
0 27 a
1 25 b
2 270 t
3 205 h


Related Topics



Leave a reply



Submit