-2

I have this python script, this is just a part of it, it works but for just 2 lines I'm having troubles:

not_marketo.loc['Marketo DEP'] = "NO"
yes_marketo.loc[:,'Marketo DEP'] = C

I have tried all the possible ways:

not_marketo['Marketo DEP'] = "NO"
not_marketo.loc['Marketo DEP'] = "NO"
not_marketo.loc[:,'Marketo DEP'] = "NO"

"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"

SettingWithCopyWarning:
pandas\core\indexing.py:1596: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value, self.name)

# Entry Point List (epl)
df_left = df_db[['Email', 'SOURCE', 'Data Entry Point', 'File']]
df_right = df_mto[['Email', 'Entry Point List', 'Marketo']]
df_epl = pd.merge(df_left, df_right, on="Email", how = 'outer')
df_epl.loc[df_epl['Marketo'].isnull(), 'Marketo'] = 'NO'
df_epl.loc[:,'Entry Point List'] = df_epl['Entry Point List'].str.replace('|',' ', regex=True)

# List of Data Entry Points from the Files
dep_list = df_epl[['Data Entry Point']]
dep_list = dep_list.dropna()
dep_list = dep_list.drop_duplicates()
list = dep_list['Data Entry Point'].tolist()

# By Groups
yes_marketo = df_epl[(df_epl['Marketo'] == "YES") & (df_epl['File'].notnull())]
not_marketo = df_epl[(df_epl['Marketo'] == "NO")  & (df_epl['File'].notnull())]
not_files   = df_epl.loc[df_epl['File'].isnull()]

# If not in Marketo not Entry Data Point
not_marketo.loc['Marketo DEP'] = "NO"

# Check Entry Point List for yes_marketo
C = []
for index, row in yes_marketo.iterrows():

    if row['Data Entry Point'] in row['Entry Point List']:
        C.append('YES')
    else:
        C.append('NO')

yes_marketo.loc[:,'Marketo DEP'] = C
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Berny
  • 113
  • 11
  • You are setting the value of multiple rows in a column with one value. Thats what the error is trying to tell you. – le_camerone Jul 23 '21 at 13:55
  • In your `# By Groups` part of the code add a `.copy()` at the end when you slice the subsets. This will force the creation of a new object which should prevent the warning in later lines when you assign values to those. – ALollz Jul 23 '21 at 13:55

1 Answers1

1

You've unsafely subset the DataFrame several times in the shown code:

# UNSAFE SUBSET HERE
dep_list = df_epl[['Data Entry Point']]

# UNSAFE SUBSET HERE
yes_marketo = df_epl[(df_epl['Marketo'] == "YES") & (df_epl['File'].notnull())]
# UNSAFE SUBSET HERE
not_marketo = df_epl[(df_epl['Marketo'] == "NO") & (df_epl['File'].notnull())]

# UNSAFE SUBSET HERE
not_files = df_epl.loc[df_epl['File'].isnull()]

Probably the easiest way to fix would be to add copy

dep_list = df_epl[['Data Entry Point']].copy()

yes_marketo = df_epl[
    (df_epl['Marketo'] == "YES") & (df_epl['File'].notnull())
    ].copy()

not_marketo = df_epl[
    (df_epl['Marketo'] == "NO") & (df_epl['File'].notnull())
    ].copy()

not_files = df_epl.loc[df_epl['File'].isnull()].copy()

Lots of details here How to deal with SettingWithCopyWarning in Pandas

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57