如何在Pandas数据框中将字符串列转换为字典类型?

lnlaulya  于 2022-12-09  发布在  其他
关注(0)|答案(1)|浏览(254)

我有一个 Dataframe df,其中包含一列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>'|

数据类型-

print(type(df['events'][0])) 
<class 'str'>
print(type(df['events']))
<class 'pandas.core.series.Series'>
print(type(df))
<class 'pandas.core.frame.DataFrame'>

如何将字符串转换为字典类型?

vyswwuz2

vyswwuz21#

您是否需要对列type to dict进行类型转换?
Pandas中没有type - dict这种东西。这种字典类型的格式在Pandas中通常被认为是str or mixed。通常根据我的知识,数据在Pandas中将被处理为向量-Intro to data structures #pandas。它只是一个以向量化格式存储的dict序列。
更简单地说-存储在数组中的字典永远不会成为dict,因为数组中的数据来自Dict

相关问题