'Even after converting a dataframe into dictionary, its type remains as a pandas dataframe
I have a dataframe like the one below
--> playstore_df['App'].value_counts()
ROBLOX 9
8 Ball Pool 7
Bubble Shooter 6
Helix Jump 6
Zombie Catchers 6
On the left we have app names, and on right, its count (i.e. how many times it has occurred in the dataset)
I stored it into a variable called app_count. To convert this into a dictionary, I used the to_dict() function.
--> app_count.to_dict()
The following was the output:
{'ROBLOX': 9,
'8 Ball Pool': 7,
'Bubble Shooter': 6,
'Helix Jump': 6,
'Zombie Catchers': 6}
But when I check the type of app_count using the following function:
--> type(app_count)
It gives the output as:
pandas.core.series.Series
Solution 1:[1]
DataFrame.to_dict() does not operate in-place. It will return a dictionary containing the DataFrame's content (like you saw when using app_count.to_dict()), but the original df will remain unchanged. To save the modifications, you need to store them back to the original variable like so:
app_count = app_count.to_dict()
Now, app_count will be a dictionary.
Side note: if you don't need the original Series/DataFrame, you can also get the dictionary in one line:
app_count = playstore_df['App'].value_counts().to_dict()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lukas Thaler |
