警告:请尝试改用.loc[row\u indexer,col\u indexer]=值

2024-04-20 04:31:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我将数据帧的一部分切片,以便只保留两列

description_category = titles[['listed_in','description']]

提取物是这样的

description_category.head()

    Listed_in                                           description
0   International TV Shows, TV Dramas, TV Sci-Fi &...   In a future where the elite inhabit an island ...
1   Dramas, International Movies                        After a devastating earthquake hits Mexico Cit...
2   Horror Movies, International Movies                 When an army recruit is found dead, his fellow...
3   Action & Adventure, Independent Movies, Sci-Fi...   In a postapocalyptic world, rag-doll robots hi...
4   Dramas                                              A brilliant group of students become card-coun...

我想做的是将[,]每个主题放在“列出的”列中,这样看起来:

    listed_in                                           description
0   [International TV Shows, TV Dramas, TV Sci-Fi ...   In a future where the elite inhabit an island ...
1   [Dramas, International Movies]                      After a devastating earthquake hits Mexico Cit...
2   [Horror Movies, International Movies]               When an army recruit is found dead, his fellow...
3   [Action & Adventure, Independent Movies, Sci-F...   In a postapocalyptic world, rag-doll robots hi...
4   [Dramas]                                            A brilliant group of students become card-coun...

我试过了,但它给了我一个警告:

description_category['listed_in'] = description_category['listed_in'].apply(lambda x: x.split(', '))

警告:

C:\Anaconda\envs\nlp_course\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  """Entry point for launching an IPython kernel.

我在这个问题上检查了几个线程,但仍然无法修复它

你建议我做什么

如果你需要更多关于我的问题的背景资料,请告诉我


1条回答
网友
1楼 · 发布于 2024-04-20 04:31:30

如果您想在保持titles的同时创建一个新的数据帧,那么

  • 带有.loc[]的任一切片:

    description_category = titles.loc[:, ['listed_in', 'description']]
    
  • 或者创建一个.copy()

    description_category = titles[['listed_in', 'description']].copy()
    

另外,使用^{}而不是apply()更快:

description_category['listed_in'] = description_category['listed_in'].str.split(', ')

相关问题 更多 >