python - SettingWithCopyWarning while using .loc -
problem simplified:
i need extract , modify particular rows of dataframe based on whether or not text within column has '-' character. dash , beyond needs removed , remaining text needs whatever preceding '-'.
have: textcol 0 no dash here 1 1 - here want: textcol 0 1 here code used recreate scenario.
df = pd.dataframe(data=['no dash here', 'one - here'], index=[0, 1], columns=['textcol']) df2 = df[df['textcol'].str.contains('-') == true] df2.loc[:, ['textcol']] = df2['textcol'].str.split('-').str[0] the resulting dataframe df2 yields result desire, 1 exception. every time call df2 (or derivative thereafter) receive following settingwithcopywarning:
a value trying set on copy of slice dataframe see caveats in documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy i tried accomplish wanted different way, , given similar error instructed me try , use .loc() functionality instead, i'm still receiving similar error.
is there better, non-error threatening way me accomplish result? i'm afraid occurring here don't understand , df2 not result in want. wondering if .query() work.
as mentioned @edchum, df2 view on df opposed copy. if want copy, can use .copy() (see docs) , settingwithcopywarning disappears:
df2 = df[df['textcol'].str.contains('-') == true].copy() df2.loc[:, ['textcol']] = df2['textcol'].str.split('-').str[0] see returning view vs copy in pandas docs.
Comments
Post a Comment