【发布时间】: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