**已关闭。**此问题需要debugging details。当前不接受答案。
编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
昨天关门了。
Improve this question
我正在使用netlix_dataset学习python。我试图根据逗号分割导演名称并创建新的 Dataframe 。我得到错误,说标题不在索引中。请帮助。
show_id,type,title,director,cast,country,date_added,release_year,rating,duration,listed_in,description
s1,Movie,Dick Johnson Is Dead,Kirsten Johnson,,United States,"September 25, 2021",2020,PG-13,90 min,Documentaries,"As her father nears the end of his life, filmmaker Kirsten Johnson stages his death in inventive and comical ways to help them both face the inevitable."
s2,TV Show,Blood & Water,,"Ama Qamata, Khosi Ngema, Gail Mabalane, Thabang Molaba, Dillon Windvogel, Natasha Thahane, Arno Greeff, Xolile Tshabalala, Getmore Sithole, Cindy Mahlangu, Ryle De Morny, Greteli Fincham, Sello Maake Ka-Ncube, Odwa Gwanya, Mekaila Mathys, Sandi Schultz, Duane Williams, Shamilla Miller, Patrick Mofokeng",South Africa,"September 24, 2021",2021,TV-MA,2 Seasons,"International TV Shows, TV Dramas, TV Mysteries","After crossing paths at a party, a Cape Town teen sets out to prove whether a private-school swimming star is her sister who was abducted at birth."
s3,TV Show,Ganglands,Julien Leclercq,"Sami Bouajila, Tracy Gotoas, Samuel Jouy, Nabiha Akkari, Sofia Lesaffre, Salim Kechiouche, Noureddine Farihi, Geert Van Rampelberg, Bakary Diombera",,"September 24, 2021",2021,TV-MA,1 Season,"Crime TV Shows, International TV Shows, TV Action & Adventure","To protect his family from a powerful drug lord, skilled thief Mehdi and his expert team of robbers are pulled into a violent and deadly turf war."
s4,TV Show,Jailbirds New Orleans,,,,"September 24, 2021",2021,TV-MA,1 Season,"Docuseries, Reality TV","Feuds, flirtations and toilet talk go down among the incarcerated women at the Orleans Justice Center in New Orleans on this gritty reality series."
df =pd.read_csv("/content/sample_data/netflix_titles.csv",index_col=0)
splitters = df['director'].apply(lambda x: str(x).split(',')).tolist()
df_director = pd.DataFrame(splitters,index=df['title']).stack()
df_director3 = pd.DataFrame(df_director)
df_director3.reset_index()
df_directors = df_director3[['title',0]]
df_directors
KeyError:“['title']不在索引中”
1条答案
按热度按时间mw3dktmi1#
我假设您希望将控制器名称拆分为逗号,并使用标题和每个控制器名称创建一个新的 Dataframe 。
尝试在stack()之后使用reset_index()使索引再次成为常规列,然后使用rename()给予新列命名:
这里,我们将列 level_1(由stack()创建)重命名为 director_index,将列0重命名为 director。最后,我们只选择 df_directors 中我们想要的列(title 和 director)。
假设输入为:
输出应该如下所示:
顺便说一句,尽量避免将数据作为图像发布。