A value is trying to be set on a copy of a slice from a DataFrame. - pandas

Viewed 35095

I'm new to pandas, and, given a data frame, I was trying to drop some columns that don't accomplish an specific requirement. Researching how to do it, I got to this structure:

df = df.loc[df['DS_FAMILIA_PROD'].isin(['CARTOES', 'CARTÕES'])]

However, when processing the frame, I get this error:

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

I'm not sure about what to do because I'm already using the .loc function. What am I missing?

f = ['ID_manifest', 'issue_date', 'channel', 'product', 'ID_client', 'desc_manifest']

df = pd.DataFrame(columns=f)
for chunk in df2017_chunks:
    aux = preProcess(chunk, f)
    df = pd.concat([df, aux])

def preProcess(df, f):    
    stops = list(stopwords.words("portuguese"))
    stops.extend(['reclama', 'cliente', 'santander', 'cartao', 'cartão'])

    df = df.loc[df['DS_FAMILIA_PROD'].isin(['CARTOES', 'CARTÕES'])]

    df.columns = f
    df.desc_manifest = df.desc_manifest.str.lower() # All lower case
    df.desc_manifest = df.desc_manifest.apply(lambda x: re.sub('[^A-zÀ-ÿ]', ' ', str(x))) # Just letters
    df.replace(['NaN', 'nan'], np.nan, inplace = True) # Remone nan
    df.dropna(subset=['desc_manifest'], inplace=True)
    df.desc_manifest = df.desc_manifest.apply(lambda x: [word for word in str(x).split() if word not in stops]) # Remove stop words

    return df
4 Answers

If your program intends to take a copy of the df on purpose, you can stop the warning with this:

pd.set_option('mode.chained_assignment', None)
Related