【问题标题】:Warning : Try using .loc[row_indexer,col_indexer] = value instead [duplicate]警告:尝试使用 .loc[row_indexer,col_indexer] = value 代替 [重复]
【发布时间】:2021-07-03 16:07:58
【问题描述】:

我对数据框的一部分进行了切片以仅保留两列。

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”列中放入[,]每个主题,所以它看起来像这样:

    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.

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

你建议我做什么?

如果您需要我的问题的更多背景信息,请告诉我。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

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

    • .loc[] 的任一切片:

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

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

    另外,使用.str.split() 比使用apply() 更快:

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

    【讨论】:

      【解决方案2】:

      试试这个..它会工作! 用 DataFrame.loc[ ]` 结束整个任务

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

      它不会显示任何警告

      【讨论】:

        猜你喜欢
        • 2018-10-06
        • 2022-11-29
        • 2017-11-28
        • 2018-12-22
        • 2021-11-30
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多