_id doc_count doc_media_url image_tagging
0 327bcc224b8c7049 1.0 URL1 {'success': True, 'tags': [], 'custom_tags': []}
1 e466c4966666c69e 1.0 URL2 {'success': True, 'tags': [{'tag': 'Cartoon', ...
2 b4303830389cf8f9 1.0 URL3 {'success': True, 'tags': [{'tag': 'Poster', '...
3 00a424323220b68e 1.0 URL4 {'success': True, 'tags': [{'tag': 'Stage', 'c...
4 c66e3e2921a7c7cd 1.0 URL5 {'success': True, 'tags': [], 'custom_tags': []}
…我的问题在于image_tagging列.目前它是一列词典.我打算将字典中的键提取到他们自己的列中,但是我被阻止了,因为一行数据不是字典而是一个列出了任何期望字典的操作的列表.
df.image_tagging.apply(lambda x: type(x)).value_counts()
<class 'dict'> 14067
<class 'list'> 1
Name: image_tagging, dtype: int64
这个列表项不应该在那里,所以我想清理那一行.但是我在按类型选择行时遇到问题,因为Pandas主要关注dtypes,而dict和列表被归类为相同(我认为无论如何!).
有没有办法可以选择该列中包含列表项的行,以便我可以从DataFrame中删除它?
谢谢你的帮助!
解决方法:
试试这个:
df = df[df.image_tagging.map(type)==dict]
演示:
In [146]: df = pd.DataFrame({
...: 'A': [{'1':1, 'a':2}, [1,2,3], {'2':2}],
...: })
In [147]: df
Out[147]:
A
0 {'1': 1, 'a': 2}
1 [1, 2, 3]
2 {'2': 2}
In [148]: df = df[df.A.map(type) == dict]
In [149]: df
Out[149]:
A
0 {'1': 1, 'a': 2}
2 {'2': 2}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。