df_masked.loc[:, col] = df_masked.groupby([df_masked.index.month, df_masked.index.day])[col].\
transform(lambda y: y.fillna(y.median()))
Run Code Online (Sandbox Code Playgroud)
即使在使用.loc之后,我也会得到这个.错误,我该如何解决?
Anaconda\lib\site-packages\pandas\core\indexing.py:476: 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.obj[item] = s
Run Code Online (Sandbox Code Playgroud)
unu*_*tbu 12
如果df_masked是某个其他DataFrame的子DataFrame,您可以获得此UserWarning.特别是,如果数据已从原始DataFrame 复制到df_masked那时,Pandas会发出UserWarning,提醒您修改df_masked不会影响原始DataFrame.
如果您不打算修改原始DataFrame,则可以忽略UserWarning.
有一些方法可以基于每个语句关闭UserWarning.特别是,你可以使用df_masked.is_copy = False.
如果你经常遇到这个UserWarning,那么我认为最好不要一个接一个地沉默UserWarning,而是在开发代码时保留它们.请注意UserWarning的含义,以及如果修改 - child-does-not-affected-the-parent问题不会影响您,请忽略它.当您的代码准备好生产时,或者如果您有足够的经验不需要警告,请完全关闭它们
pd.options.mode.chained_assignment = None
Run Code Online (Sandbox Code Playgroud)
靠近代码顶部.
这是一个简单的例子,它说明了问题和(a)解决方案:
import pandas as pd
df = pd.DataFrame({'swallow':['African','European'], 'cheese':['gouda', 'cheddar']})
df_masked = df.iloc[1:]
df_masked.is_copy = False # comment-out this line to see the UserWarning
df_masked.loc[:, 'swallow'] = 'forest'
Run Code Online (Sandbox Code Playgroud)
UserWarning存在的原因是为了帮助警告新用户链接索引这样的事实
df.iloc[1:].loc[:, 'swallow'] = 'forest'
Run Code Online (Sandbox Code Playgroud)
不会影响df第一个索引器(例如df.iloc[1:])的结果何时返回副本.
| 归档时间: |
|
| 查看次数: |
4777 次 |
| 最近记录: |