fillna()函数填充报错SettingWithCopyWarning

探讨使用Pandas处理数据集中的缺失值时遇到的SettingWithCopyWarning警告,尝试多种解决方案,包括使用loc函数和字典填充方法,但仍未能完全避免警告信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

X['Age'].fillna(X['Age'].mean(), inplace=True)运行后会出现

Warning (from warnings module):
  File "D:\Python\Python27\lib\site-packages\pandas\core\generic.py", line 6130
    self._update_inplace(new_data)
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

报错原因:

这是因为凡是出现链式赋值(例x=y=z=1)的情况,pandas不确定到底返回的是引用还是拷贝。所以干脆报warning,我上边那个函数就是对age列的缺失值全部赋为平均值。

解决方法:

1. 上网查了一下方法很多都说用loc函数,X.loc[:,['Age']].fillna(X['Age'].mean(), inplace=True),虽然不报警了但根本没有真正赋值,缺失值还是NaN(可能是我没有用对方法)。

2. 后来在stackoverflow上找到了另一个解决方法,原文地址:Pandas won't fillna() inplace

X.fillna({'Age':X['Age'].mean()}, inplace=True),用字典就可以完成填充但是无法解决报警信息。。。

希望能有人告知两全其美的解决方法!!!>﹏<

D:\泰坦尼克号生还者预测\泰坦尼克号生还者预测.py:13: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. df['Age'].fillna(df['Age'].median(), inplace=True) D:\泰坦尼克号生还者预测\泰坦尼克号生还者预测.py:14: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. df['Embarked'].fillna('S', inplace=True) D:\泰坦尼克号生还者预测\泰坦尼克号生还者预测.py:13: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. df['Age'].fillna(df['Age'].median(), inplace=True) D:\泰坦尼克号生还者预测\泰坦尼克号生还者预测.py:14: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. df['Embarked'].fillna('S', inplace=True) 这些报错是什么意思;但是最后运行出的结果得分0.75;可以不修改直接交吗
06-13
import pandas as pd # 读取 Excel 文件(注意添加r前缀) df = pd.read_excel(r"D:\cleaned.xlsx") # 强制转换frame_id为整型 df['frame_id'] = pd.to_numeric(df['frame_id'], errors='coerce').astype('Int64') # 筛选数据(保持原有逻辑) filtered_df = df[(df['yp'] > -3.5) & (df['yp'] <= 0)].copy() # 计算是否有紧急车辆标记 has_emergency filtered_df.loc[:, 'has_emergency'] = filtered_df.groupby('frame_id')['cls_id'].transform( lambda x: x.eq(8).any() ) def calculate_spacing(group): """按车道排序并计算平均车头间距""" sorted_vehicles = group.sort_values("xp") spacings = sorted_vehicles["xp"].diff().dropna() # 计算相邻车辆之间的距离差值 return spacings.mean() if not spacings.empty else None # 返回非空时的均值 # 按照是否为紧急状态 + 时间帧分组计算车头间距 spacing_by_group = ( filtered_df.groupby(['has_emergency', 'frame_id'], observed=True) .apply(calculate_spacing) ) # 将结果转换为 DataFrame 格式 result_df = spacing_by_group.reset_index(name='平均车头间距(m)').fillna({'平均车头间距(m)': 0}) import matplotlib.pyplot as plt # 按frame_id排序保证时间连续性 result_sorted = result_df.sort_values('frame_id') # 创建画布 plt.figure(figsize=(15, 6)) # 绘制完整折线(浅蓝色背景线) plt.plot(result_sorted['frame_id'], result_sorted['平均车头间距(m)'], color='lightblue', linewidth=1, label='基础间距') # 突出显示有应急车辆的帧(红色圆点标记) emergency_frames = result_sorted[result_sorted['has_emergency']] plt.scatter(emergency_frames['frame_id'], emergency_frames['平均车头间距(m)'], color='red', s=30, label='存在应急车辆', zorder=3) # 设置图表格式 plt.title('车头间距随时间变化趋势(应急车辆标记)', fontsize=14, pad=20) plt.xlabel('时间帧ID', fontsize=12) plt.ylabel('平均车头间距(m)', fontsize=12) plt.grid(True, linestyle='--', alpha=0.5) plt.legend(loc='upper right') # 自动调整刻度密度 plt.xticks(rotation=45) plt.tight_layout() plt.show()报错,完善代码
04-02
D:\Anaconda\python.exe D:\泰坦尼克号生还者预测\泰坦尼克号生还者预测.py D:\泰坦尼克号生还者预测\泰坦尼克号生还者预测.py:19: SyntaxWarning: invalid escape sequence '\.' combined['Title'] = combined['Name'].str.extract(' ([A-Za-z]+)\.', expand=False) D:\泰坦尼克号生还者预测\泰坦尼克号生还者预测.py:14: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. combined['Age'].fillna(age_median, inplace=True) D:\泰坦尼克号生还者预测\泰坦尼克号生还者预测.py:15: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. combined['Embarked'].fillna(combined['Embarked'].mode()[0], inplace=True) D:\泰坦尼克号生还者预测\泰坦尼克号生还者预测.py:16: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. combined['Fare'].fillna(combined['Fare'].median(), inplace=True) 这是什么问题
06-13
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值