简体   繁体   English

SettingWithCopyWarning:试图在来自 DataFrame 的切片副本上设置一个值警告

[英]SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame WARNING

Help.帮助。 I tried to run the code below:我试图运行下面的代码:

import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
import statsmodels.stats.multicomp as multi

#import data
df = pd.read_csv('nesarc2.csv', low_memory=False, na_values=' ')

#setting needed variables to numeric
df['CHECK321'] = pd.to_numeric(df['CHECK321'], errors='coerce')
df['S3AQ3B1'] = pd.to_numeric(df['S3AQ3B1'], errors='coerce')
df['S3AQ3C1'] = pd.to_numeric(df['S3AQ3C1'], errors='coerce')
df['S4AQ4A5'] = pd.to_numeric(df['S4AQ4A5'], errors='coerce')

#SETTING MISSING DATA
df['CHECK321']=df['CHECK321'].replace(9, np.nan)
df['S3AQ3B1']=df['S3AQ3B1'].replace(9, np.nan)
df['S3AQ3C1']=df['S3AQ3C1'].replace(99, np.nan)
df['S4AQ4A5']=df['S4AQ4A5'].replace(9, np.nan)
    
#subset data to young adults age 18 to 25 who have smoked in the past 12 months
data=df[(df['AGE']>=18) & (df['AGE']<=25) & (df['CHECK321']==1)]

#recoding number of days smoked in the past month
recode = {1: 30, 2: 22, 3: 14, 4: 5, 5: 2.5, 6: 1}
data['USFREQMO']= data['S3AQ3B1'].map(recode, inplace = True)

Then I got the error below:然后我得到以下错误:

C:\Users\Adeagbo Rapheal\anaconda3\lib\site-packages\pandas\core\generic.py:6746: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

I haven't been able to work with the variable even when I ignored the warning and moved on with the analysis.即使我忽略了警告并继续分析,我也无法使用该变量。

Let us do fixing让我们做修复

data=df[(df['AGE']>=18) & (df['AGE']<=25) & (df['CHECK321']==1)].copy()

data['USFREQMO'] = data['S3AQ3B1'].map(recode)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas DataFrame:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值 - Pandas DataFrame: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame SettingWithCopyWarning:试图在DataFrame的切片副本上设置一个值 - SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame 对于循环错误,SettingWithCopyWarning:正在尝试在 DataFrame 中的切片副本上设置值 - For loop error, SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame Python:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值 - Python: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame 更改熊猫列中的值:SettingWithCopyWarning:正在尝试在数据帧错误中的切片副本上设置值 - Changing values in pandas columns: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame error 循环导致错误:“SettingWithCopyWarning:试图在 DataFrame 的切片副本上设置值” - Loop led to Error: 'SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame' Python Pandas 警告:试图在 DataFrame 的切片副本上设置值 - Python Pandas Warning: A value is trying to be set on a copy of a slice from a DataFrame 试图在来自 DataFrame 警告的切片副本上设置值 - A value is trying to be set on a copy of a slice from a DataFrame Warning SettingWithCopyWarning:试图在 DataFrame 中切片的副本上设置值。 尝试使用 .loc[row_indexer,col_indexer] = value 代替, - 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, 试图在 DataFrame 中的切片副本上设置一个值 - A value is trying to be set on a copy of a slice from a DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM