首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >警告:尝试使用.loc[row_indexer,col_indexer] = value

警告:尝试使用.loc[row_indexer,col_indexer] = value
EN

Stack Overflow用户
提问于 2021-07-03 16:07:58
回答 2查看 5.7K关注 0票数 1

我将数据文件的一部分切片,只保留两列。

代码语言:javascript
复制
description_category = titles[['listed_in','description']]

摘录是这样的

代码语言:javascript
复制
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“专栏中插入每个主题,所以看起来是这样的:

代码语言:javascript
复制
    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...

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

代码语言:javascript
复制
description_category['listed_in'] = description_category['listed_in'].apply(lambda x: x.split(', '))

警告:

代码语言:javascript
复制
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.

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

你建议我怎么做?

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-03 16:24:25

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

  • 要么用.loc[]切片:

'description'] = description_category = titles.loc[:,'listed_in',

  • 或创建.copy()

description_category =标题[‘listed_in’,description_category

另外,使用.str.split()而不是apply()更快

代码语言:javascript
复制
description_category['listed_in'] = description_category['listed_in'].str.split(', ')
票数 2
EN

Stack Overflow用户

发布于 2022-02-27 02:29:04

试试这个..。会成功的!!用DataFrame.loc‘结束整个任务

代码语言:javascript
复制
description_category.loc[description_category['listed_in'] = description_category['listed_in'].apply(lambda x: x.split(', '))]

它不会显示任何警告

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68237941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档