Pandas DataFrame to List of Dictionaries
Use df.to_dict('records')
-- gives the output without having to transpose externally.
In [2]: df.to_dict('records')
Out[2]:
[{'customer': 1L, 'item1': 'apple', 'item2': 'milk', 'item3': 'tomato'},
{'customer': 2L, 'item1': 'water', 'item2': 'orange', 'item3': 'potato'},
{'customer': 3L, 'item1': 'juice', 'item2': 'mango', 'item3': 'chips'}]
Convert list of dictionaries to a pandas DataFrame
If ds
is a list of dict
s:
df = pd.DataFrame(ds)
Note: this does not work with nested data.
how to convert a pandas dataframe to a list of dictionaries in python?
You can groupby and then use to_dict to convert it to a dictionary.
>>> df.groupby(df['id'], as_index=False).agg(list).to_dict(orient="records")
[{'id': 1, 'value': ['a', 'b']}, {'id': 2, 'value': ['c', 'd', 'e']}, {'id': 3, 'value': ['f']}]
Extract value from list of dictionaries in dataframe using pandas
df['ID'] = df['Resources'].apply(lambda x: ','.join([i['resourceName'] for i in eval(x)]))
Date ... ID
0 28-02-2022 ... i-05fbb7a
1 28-02-2022 ... i-08bd2475,i-0fd69dc1,i-0174dd38aea
Create a pandas DataFrame from a list of dictionaries with dictionary keys set as row labels
Try this1
# build a nested dict from list_example and build df
df = pd.DataFrame.from_dict({k: {'time': v} for d in list_example for k,v in d.items()}, orient='index')
print(df)
time
companies_info_5000_5100 121.201472
companies_info_5100_5200 116.492211
1: This method doesn't build a dataframe for each row. Reformatting the dictionary will be much more efficient than the method in the OP. For example, for a dataframe with 10000 rows, this solution takes 24.3 ms while the one in the OP take 4s (this one is 164 times faster).
How to make pandas DataFrame from list of dictionaries
You can do:
dict_list = [{0: 0.05, 0.5: 0.15, 1: 0.1}, {0: 0.06, 0.5: 0.14, 1: 0.2}, {0: 0.09, 0.5: 0.25, 1: 0.17}]
df = pd.DataFrame(dict_list).T
df.columns = (df.columns + 1).map('Dict{}'.format)
print(df):
Dict1 Dict2 Dict3
0.0 0.05 0.06 0.09
0.5 0.15 0.14 0.25
1.0 0.10 0.20 0.17
If you want index to be a separate column you can do:
df = df.reset_index().rename(columns={'index':'Index'})
Convert pandas.DataFrame to list of dictionaries in Python
pandas.DataFrame.to_json
returns a string (JSON string), not a dictionary. Try to_dict
instead:
>>> df
col1 col2
0 1 3
1 2 4
>>> [df.to_dict(orient='index')]
[{0: {'col1': 1, 'col2': 3}, 1: {'col1': 2, 'col2': 4}}]
>>> df.to_dict(orient='records')
[{'col1': 1, 'col2': 3}, {'col1': 2, 'col2': 4}]
Related Topics
Replacing Few Values in a Pandas Dataframe Column with Another Value
List to Dictionary Conversion with Multiple Values Per Key
How to Make a Tkinter Window Jump to the Front
List Comprehension VS Generator Expression's Weird Timeit Results
Convert Pandas Dataframe to Nested JSON
How to Remove Nan Value While Combining Two Column in Panda Data Frame
Matplotlib Y Axis Values Are Not Ordered
Replace() Method Not Working on Pandas Dataframe
Df.Append() Is Not Appending to the Dataframe
Pandas Dataframe Str.Contains() and Operation
Attributeerror: Module 'Time' Has No Attribute 'Clock' in Python 3.8
Collapse Multiple Submodules to One Cython Extension
How to Know the Angle Between Two Vectors
How to Set the Value of a Pandas Column as List