Python 清理数据帧列
我试图从我的数据框中的一列中删除所有不相关的信息,但不知道如何做。我将数据从CSV文件直接导入数据框 因此,目前我的“流派”专栏如下所示:Python 清理数据帧列,python,pandas,dataframe,Python,Pandas,Dataframe,我试图从我的数据框中的一列中删除所有不相关的信息,但不知道如何做。我将数据从CSV文件直接导入数据框 因此,目前我的“流派”专栏如下所示: {'genres': {0: [{"id": 28, "name": "Action"}, {"id": 12, "name": 'Fantasy'}, etc. {'genres': {0: "['Action', 'Fantasy']"}, etc. 我希望它看起来像这样: {'genres': {0: [{"id": 28, "name": "A
{'genres': {0: [{"id": 28, "name": "Action"}, {"id": 12, "name": 'Fantasy'}, etc.
{'genres': {0: "['Action', 'Fantasy']"}, etc.
我希望它看起来像这样:
{'genres': {0: [{"id": 28, "name": "Action"}, {"id": 12, "name": 'Fantasy'}, etc.
{'genres': {0: "['Action', 'Fantasy']"}, etc.
如果有人能帮助我或按正确的方向发送我,我们将不胜感激。基于描述中唯一的索引,也许这就是您要寻找的,
分解,转换成系列,分组方式
使用索引,列出所有名称:
data = {'genres':
{0: [{"id": 28, "name": "Action"}, {"id": 12, "name": 'Fantasy'}],
1: [{"id": 40, "name": "Crime"}, {"id": 24, "name": 'Thriller'}]
}}
df = pd.DataFrame(data)
df.head()
# genres
# 0 [{'id': 28, 'name': 'Action'}, {'id': 12, 'nam...
# 1 [{'id': 40, 'name': 'Crime'}, {'id': 24, 'name...
执行上述步骤:
df['genres_fix'] = df.genres.explode().transform(
pd.Series).groupby(level=0)['name'].apply(list)
df[['genres_fix']].head()
# genres_fix
# 0 [Action, Fantasy]
# 1 [Crime, Thriller]
您应该使用df.head()。\u dict()
将其粘贴为问题中的数据,而不是粘贴图像,请检查并完成,谢谢!你有什么建议吗?那是一本有效的字典吗?我还不能复制。您应该能够pd.DataFrame(d)
来重现数据帧。另外,请在production\u country
列的生成基础上发布预期产出的样子?您在那里发布的内容没有用处。请使用df.head()。\u剪贴板()
,然后进入SO。