I try to filter Pandas DataFrame:
df = pd.read_csv('ml_data.csv', dtype=str)
def df_filter(df):
#df = df.copy()
df.replace('(not set)', '(none)', inplace=True) #comment this and warning will disappear!!!
df = df[df['device_browser'] != '(none)'] #comment this and warning will disappear!!!
def browser_filter(s):
return ''.join([c for c in s if c.isalpha()])
df['device_browser'] = df['device_browser'].apply(browser_filter)
return df
df = df_filter(df)
And I receive this warning:
/tmp/ipykernel_2185/1710484338.py:11: 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
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df['device_browser'] = df['device_browser'].apply(browser_filter)
But if I uncomment
#df = df.copy()
OR comment
df.replace('(not set)', '(none)', inplace=True)
OR comment
df = df[df['device_browser'] != '(none)']
OR will not wrap filtering in df_filter function
this warning will disappear!!! WHY??????????
I danced around the fire and beat the tambourine...