1

I have a Dataframe, and one column is full of lists of words. I am using a function to remove all words that are not in a secondary list. When I use apply to trim my lists I get a warning telling me I should use .loc but I'm not sure how to use it properly with apply.

here is my function

    def trimTo20(tweet):   

        for check in reversed(tweet):
            if check not in over20Hash:
                tweet.remove(check)

        return(tweet)

here is my call

    group20.sumText = group20.sumText.apply(trimTo20)

and here is the warning.

    Anaconda3\lib\site-packages\pandas\core\generic.py:3110: 
    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: http://pandas.pydata.org/pandas-
    docs/stable/indexing.html#indexing-view-versus-copy
      self[name] = value

and here is a slice of my Dataframe

        UserID  sumText
    3   1644    [#life, #contentmarketing, #ContentMarketing]
    5   1737    [@SwiftOnSecurity, @erinscafe, @ComfortablySmug]
    8   2391    [@, @MikeIsaac, @fmanjoo, @rtraister, @TheCut, @twitter]
    10  2426    [@MikeIsaac, @GOP, @united, @facebook, @gruber]

everything appears fine in my dataframe, should I be concerned that some data didn't copy over correctly? I've used apply a couple times this way and never gotten this warning.

SpecialK711
  • 48
  • 1
  • 8
  • No worries, take a look at this https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas – Shubham R Mar 06 '18 at 05:20
  • also take a look at [`df.assign()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html) – Vivek Kalyanarangan Mar 06 '18 at 05:30
  • This error is a bit confused, becaue problem is in line code above `group20.sumText = group20.sumText.apply(trimTo20)`. If there is filtering, is necessary add `.copy()` like `group20 = df[mask].copy()` – jezrael Mar 06 '18 at 06:26

0 Answers0