OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Create a new pandas column with a value from a user-defined function

  • Thread starter Thread starter ajscriv
  • Start date Start date
A

ajscriv

Guest
I am getting a SettingWithCopyWarning when attempting to create a new column on a pandas dataframe using a function I created to return a value for that new column. I am using the movielens dataset and predicting the rating of a user on a movie.

This is an example of my dataframe:

enter image description here

Now if I want to add a new column called 'prediction' that sends the user_id and item_id to my function and return the prediction I have followed the advice of this other question

Hence using the code:

Code:
df['pred'] = df.apply(lambda x: predict_rating(x['user_id'], x['item_id']), axis =1)

Yet I keep getting the SettingWithCopyWarning.

:44: 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

Any advice would be welcome.
<p>I am getting a SettingWithCopyWarning when attempting to create a new column on a pandas dataframe using a function I created to return a value for that new column. I am using the movielens dataset and predicting the rating of a user on a movie.</p>

<p>This is an example of my dataframe:</p>

<p><a href="https://i.sstatic.net/kLwFs.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kLwFs.jpg" alt="enter image description here"></a></p>

<p>Now if I want to add a new column called 'prediction' that sends the user_id and item_id to my function and return the prediction I have followed the advice of this other <a href="https://stackoverflow.com/questions...tiple-arguments-to-create-a-new-pandas-column">question</a></p>

<p>Hence using the code:</p>

<pre><code>df['pred'] = df.apply(lambda x: predict_rating(x['user_id'], x['item_id']), axis =1)
</code></pre>

<p>Yet I keep getting the SettingWithCopyWarning.</p>

<p><em>:44: 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: <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy" rel="nofollow noreferrer">http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy</a></em></p>

<p>Any advice would be welcome.</p>
 
Top