How to convert dataframe to dictionary in pandas WITHOUT index
Solution 1
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.
Solution 2
dict1 = df.to_dict('records')
or
dict2 = df.to_dict('list')
list
: keys are column names, values are lists of column data
records
: each row becomes a dictionary where key is column name and value is the data in the cell
Solution 3
you can do something like this:
data.to_dict('list')
#output:
#{'Feeling low in energy-slowed down': [2, 4, 2, 4]}
Solution 4
if its just 1 column, slice the 1 column (it gets converted to Series) wrapping in a dict function
dict( myDF.iloc[:, -1] )
# [: , -1] means: return all rows, return last column)
{Jason: 25.1}
Comments
-
Symphony over 2 years
I have a dataframe
df
as follows:| name | coverage | |-------|----------| | Jason | 25.1 |
I want to convert it to a dictionary. I used the following command in
pandas
:dict=df.to_dict()
The output of
dict
gave me the following:{'coverage': {0: 25.1}, 'name': {0: 'Jason'}}
I do not want the
0
in my output. I believe this is captured because of the column index in my dataframedf
. What can I do to eliminate0
in my output ( I do not want index to be captured.) expected output :{'coverage': 25.1, 'name': 'Jason'}
-
user528025 about 4 yearsgreat, magic variables.Thank for your answer, it was exactly what I was looking for :)
-
Deepak almost 4 yearsAwesome..this is the answer I was looking for
-
Julek about 3 yearsThanks! Pandas is great but sometimes so weird in its choices.
-
artemis about 2 yearsThis just fixed an issue I had been having for hours. Thank you.