how to convert string column to dictionary type in pandas dataframe?
I have a dataframe df
with a column events
-
|events|
|{'id': 109421132110384, 'created_at': datetime.datetime(2022, 11, 28, 11, 12, 50, tzinfo=tzutc()), 'in_reply_to_id': None, 'in_reply_to_account_id': None, 'sensitive': False, 'spoiler_text': '', 'visibility': 'public', 'language': 'en', 'uri': 'https://users/statuses/10942113190455'},'note':'<p>Google me. Now go to page 8. Nope, that\'s not me. Try page 12.</p><p>-------------------------------</p><p>Would talk about <a href="https://mastodon.world/tags/parenting" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>parenting</span></a> all day, if you let me.)</p>'|
|{'id': 109421132340384, 'created_at': datetime.datetime(2022, 11, 30, 11, 12, 50, tzinfo=tzutc()), 'in_reply_to_id': None, 'in_reply_to_account_id': None, 'sensitive': False, 'spoiler_text': '', 'visibility': 'public', 'language': 'en', 'uri': 'https://users/statuses/10942113190467'},'note':'<p>Google me. Now go to page 8. Nope, that\'s not me. Try page 12.</p><p>-------------------------------</p><p>Would talk about <a href="https://mastodon.world/tags/parenting" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>parenting</span></a> all day, if you let me.)</p>'|
Datatypes -
开发者_运维百科print(type(df['events'][0]))
<class 'str'>
print(type(df['events']))
<class 'pandas.core.series.Series'>
print(type(df))
<class 'pandas.core.frame.DataFrame'>
How can we convert the string to the dictionary type?
Your need to typecast column type to dict
?
There is no such thing called type - dict
in pandas. These kind of dictionary type formats normally considered as str or mixed
in pandas. Normally as per my knowledge data will be proccesed as vectors in pandas - Intro to data structures #pandas. Its simply a dict
sequence stored in vectorised formats.
More simply - A dictionary stored inside an array will never become dict as data inside the array frommated as a Dict
精彩评论