【发布时间】:2021-03-03 03:38:27
【问题描述】:
我从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()
【问题讨论】: