2021年3月2日星期二

duplicating rows by splitting comma separated multiple values in another column pandas

I found code that should work from NameError: name 'Series' is not defined

But I get an error "name 'Series' is not defined". It worked fine in the example, but this error did come up for other users as well. Does anyone know how to make it work?

Any help would be appreciated!

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()  
https://stackoverflow.com/questions/66450416/duplicating-rows-by-splitting-comma-separated-multiple-values-in-another-column March 03, 2021 at 11:38AM

没有评论:

发表评论