Convert a Pandas DataFrame to a dictionary
The to_dict()
method sets the column names as dictionary keys so you'll need to reshape your DataFrame slightly. Setting the 'ID' column as the index and then transposing the DataFrame is one way to achieve this.
to_dict()
also accepts an 'orient' argument which you'll need in order to output a list of values for each column. Otherwise, a dictionary of the form {index: value}
will be returned for each column.
These steps can be done with the following line:
>>> df.set_index('ID').T.to_dict('list')
{'p': [1, 3, 2], 'q': [4, 3, 2], 'r': [4, 0, 9]}
In case a different dictionary format is needed, here are examples of the possible orient arguments. Consider the following simple DataFrame:
>>> df = pd.DataFrame({'a': ['red', 'yellow', 'blue'], 'b': [0.5, 0.25, 0.125]})
>>> df
a b
0 red 0.500
1 yellow 0.250
2 blue 0.125
Then the options are as follows.
dict - the default: column names are keys, values are dictionaries of index:data pairs
>>> df.to_dict('dict')
{'a': {0: 'red', 1: 'yellow', 2: 'blue'},
'b': {0: 0.5, 1: 0.25, 2: 0.125}}
list - keys are column names, values are lists of column data
>>> df.to_dict('list')
{'a': ['red', 'yellow', 'blue'],
'b': [0.5, 0.25, 0.125]}
series - like 'list', but values are Series
>>> df.to_dict('series')
{'a': 0 red
1 yellow
2 blue
Name: a, dtype: object,
'b': 0 0.500
1 0.250
2 0.125
Name: b, dtype: float64}
split - splits columns/data/index as keys with values being column names, data values by row and index labels respectively
>>> df.to_dict('split')
{'columns': ['a', 'b'],
'data': [['red', 0.5], ['yellow', 0.25], ['blue', 0.125]],
'index': [0, 1, 2]}
records - each row becomes a dictionary where key is column name and value is the data in the cell
>>> df.to_dict('records')
[{'a': 'red', 'b': 0.5},
{'a': 'yellow', 'b': 0.25},
{'a': 'blue', 'b': 0.125}]
index - like 'records', but a dictionary of dictionaries with keys as index labels (rather than a list)
>>> df.to_dict('index')
{0: {'a': 'red', 'b': 0.5},
1: {'a': 'yellow', 'b': 0.25},
2: {'a': 'blue', 'b': 0.125}}
How to convert dataframe to dictionary in pandas WITHOUT index
When I see your dataset with 2 columns I see a series and not a dataframe.
Try this: d = df.set_index('name')['coverage'].to_dict()
which will convert your dataframe to a series and output that.
However, if your intent is to have more columns and not a common key you could store them in an array instead using 'records'. d = df.to_dict('r')
.
`
Runnable code:
import pandas as pd
df = pd.DataFrame({
'name': ['Jason'],
'coverage': [25.1]
})
print(df.to_dict())
print(df.set_index('name')['coverage'].to_dict())
print(df.to_dict('r'))
Returns:
{'name': {0: 'Jason'}, 'coverage': {0: 25.1}}
{'Jason': 25.1}
[{'name': 'Jason', 'coverage': 25.1}]
And one more thing, try to avoid to use variable name dict as it is reserved.
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
Convert a pandas dataframe to dictionary with one column as key and other column as multiple values
Assuming your data frame is variable name is "df" then below may help :
temp = df.groupby(['labels']).apply(lambda x: x['tweets'].tolist()).to_dict()
print(temp)
How to convert pandas dataframe to nested dictionary
I think you were very close.
Use groupby
and to_dict
:
df = df.groupby('Name')[['Chain','Food','Healthy']]
.apply(lambda x: x.set_index('Chain').to_dict(orient='index'))
.to_dict()
print (df)
{'George': {'KFC': {'Healthy': False, 'Food': 'chicken'},
'McDonalds': {'Healthy': False, 'Food': 'burger'}},
'John': {'McDonalds': {'Healthy': True, 'Food': 'salad'},
'Wendys': {'Healthy': False, 'Food': 'burger'}}}
Converting pandas dataframe to dictionary with same keys over multiple rows
def df_to_dict(df):
# create a dictionary
d = {}
# iterate over the rows
for index, row in df.iterrows():
# if the key is not in the dictionary, add it
if row[0] not in d:
d[int(row[0])] = []
# add the tuple (row[1], row[2]) to the list associated with the key
d[row[0]].append((row[1], row[2]))
return d
print(df_to_dict(df))
How to convert rows in DataFrame in Python to dictionaries
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}]
Convert Pandas dataframe to a dictionary with first column as key
Try:
x = df.set_index("A").to_dict("index")
print(x)
Prints:
{'x1': {'B': 'x', 'C': ['x', 'y']}, 'x2': {'B': 'a', 'C': ['b', 'c', 'd']}}
Related Topics
How to Resize an Image Using Pil and Maintain Its Aspect Ratio
Installing Specific Package Version With Pip
How to Make One Python File Run Another
Choosing the Correct Upper and Lower Hsv Boundaries For Color Detection With'Cv::Inrange' (Opencv)
How to Fix: "Unicodedecodeerror: 'Ascii' Codec Can't Decode Byte"
How to Get Time of a Python Program'S Execution
Remove Empty Strings from a List of Strings
How to Convert an Rgb Image into Grayscale in Python
How to Input a Regex in String.Replace
Convert Python Dict into a Dataframe
Why Should We Not Use Sys.Setdefaultencoding("Utf-8") in a Py Script
How to Fill Out a Python String With Spaces
How to Test If a String Contains One of the Substrings in a List, in Pandas
Refering to a Directory in a Flask App Doesn't Work Unless the Path Is Absolute