【问题标题】:replacing a column's values (if it meets a condition) from another existing column's values从另一个现有列的值替换列的值(如果它满足条件)
【发布时间】:2019-02-28 20:47:38
【问题描述】:

我有一个data frame,它由 3 列组成:

Id, Summary, Description

我想要做的是如果Description 中的任何值与此字符串完全匹配:“这是一个空描述”,然后将这些内容替换为Summary 的内容。

例如:

之前:

   Id     Summary         Description
0  1      Cool song       This is an empty description
1  2      It was ok       was ok because needed more melody
2  3      this was sick   This is an empty description
3  4      not a fan       i prefer classical over rock
4  5      alright         This is an empty description

之后:

      Id     Summary         Description
   0  1      Cool song       Cool song
   1  2      It was ok       was ok because needed more melody
   2  3      this was sick   this was sick
   3  4      not a fan       i prefer classical over rock
   4  5      alright         alright

我正在使用的代码有效,但我想知道是否有更好的方法,因为我收到警告:

输入:

 df.Description = np.where(df.Description == "This is an empty description", df.Summary, df.Description)

输出:

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py:3643: 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
  self[name] = value

【问题讨论】:

  • 我也想知道为什么type(np.where(df.Description == "This is an empty description", df.Summary, df.Description)) 的输出是numpy.ndarray,而当我这样做时type(df) 说它是pandas.core.frame.DataFrame

标签: python-3.x pandas dataframe series


【解决方案1】:

当您在Pandas 中链接多个索引操作时,这是一个非常常见的警告。你可以详细阅读它here。如果您想利用 Pandas 原生方法,您可以执行以下操作而不会出现任何错误。

import pandas as pd

mask = (df.Description == "This is an empty description")
df.loc[mask, 'Description'] = df.Summary

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 2017-10-14
    • 2021-08-25
    • 2020-05-16
    • 1970-01-01
    相关资源
    最近更新 更多