我发现应该从NameError: name 'Series' is not defined开始工作的代码
但我得到一个错误“名称‘序列’未定义”。在本例中,它工作得很好,但其他用户也出现了这个错误。有人知道怎么做吗
任何帮助都将不胜感激
original_df = DataFrame([{'country': 'a', 'title': 'title1'},
{'country': 'a,b,c', 'title': 'title2'},
{'country': 'd,e,f', 'title': 'title3'},
{'country': 'e', 'title': 'title4'}])
desired_df = DataFrame([{'country': 'a', 'title': 'title1'},
{'country': 'a', 'title': 'title2'},
{'country': 'b', 'title': 'title2'},
{'country': 'c', 'title': 'title2'},
{'country': 'd', 'title': 'title3'},
{'country': 'e', 'title': 'title3'},
{'country': 'f', 'title': 'title3'},
{'country': 'e', 'title': 'title4'}])
#Code I used:
desired_df = pd.concat(
[
Series(row["title"], row["country"].split(","))
for _, row in original_df.iterrows()
]
).reset_index()
您可以在此处将^{} 与^{} 一起使用
对于
NameError
,可以通过这种方式使用导入注意:使用上述导入语句只会将
DataFrame
和Series
类带入范围首先
split
在逗号上单击列以获取列表,然后可以explode
该系列列表。将'title'
移动到索引,以便对'country'
中的每个元素重复它。最后两部分只是清理名称并从索引中删除标题另外,您的原始代码在逻辑上很好,但您需要正确地创建对象。我建议导入模块而不是单个的类/方法,这样您就可以创建一个
Series
带有pd.Series
而不是Series
相关问题 更多 >
编程相关推荐