1

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.

SmatRalph
  • 21
  • 5
  • data=df[(df['AGE']>=18) & (df['AGE']<=25) & (df['CHECK321']==1)].copy() – BENY Sep 09 '20 at 21:13
  • It runs for that particular code but at the preceding ones, it gives this error `TypeError: map() got an unexpected keyword argument 'inplace'` – SmatRalph Sep 09 '20 at 22:50
  • Check the answer – BENY Sep 09 '20 at 22:54
  • Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas?](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – Trenton McKinney Sep 09 '20 at 22:56

1 Answers1

2

Let us do fixing

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

data['USFREQMO'] = data['S3AQ3B1'].map(recode)
BENY
  • 317,841
  • 20
  • 164
  • 234