0

1I have created 3 data frames from a JSON file. I'm trying to create a column on my working data frame by mapping the key column to the other 2 data frames, the method I use works, but it throws up a warning "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. How do I go around this?

Error image

1

vimuth
  • 5,064
  • 33
  • 79
  • 116
  • At some point _before_ this provided code you have unsafely subset your DataFrame. Either `fpl_df = df[cols]` or `fpl_df = df[mask]` when it should have been `fpl_df = df[cols].copy()` or `fpl_df = df[mask].copy()` The warning is letting you know that `df[mask]['col'] = value` may not work because `df[mask]` may produce a copy and recommends that you use `df.loc[mask, 'col'] = value` but that message is not clear here since you're doing something like `fpl_df = df[mask]` then later doing `fpl_df[col] = value` which looks to pandas like a (deferred) `df[mask][col] = value` call. – Henry Ecker Aug 04 '22 at 01:35
  • So you can either [suppress the warning](/a/53954986/15497888) if it's not causing issues, or you can [fix the problem](/a/66362915/15497888) by making it an explicit copy. – Henry Ecker Aug 04 '22 at 01:36

1 Answers1

0

Alternatively, you can use pandas merge function to join the dataframes.

if you are just bothered by warning messages and if you don't like to see them, you can suppress them by the following code

import warnings
warnings.filterwarnings('ignore')

Good luck!

Ranadheer
  • 31
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 03 '22 at 04:43