1

I working with DataFrames a lot and sometimes I need to replace values of whole column to another. Usually I got a SettingWithCopyWarning. So if I got it i just copy my df and work is progressing.

Example:

df
name male
sd   Female
as   Male
we   Female
dd   Female

df = df.copy()
df['male'] = df['male'].replace({'Female':'F', 'Male':'M'})

result:

df
name male
sd   F
as   M
we   F
dd   F

So I copied my frame and changing values on copy. If I understand good I copy a frame. So I using 2x memory? And even If I omit df = df.copy() my code work fine but I got error info. Output still is ok.

So... I didn't found clear enough explanation, so I'm asking here. Is a good practice to copy my frames? Maybe is a different, better way?

martin
  • 1,145
  • 1
  • 7
  • 24
  • 1
    `df = df.copy()` is ... useless. It won't use 2x memory because you're assigning a copy to the same variable but it might use time for nothing (and may consume temporarily a few more memory). – Corentin Limier Jan 07 '20 at 12:48
  • @CorentinLimier Thanks for memory explanation. Ok, so it's just an info : "dude, u changing ur dataframe. Maybe create copy" ? And that's all? – martin Jan 07 '20 at 12:51
  • 2
    It's the opposite. Quoting from the link above : `SettingWithCopyWarning is to show users (and especially new users) that they may be operating on a copy and not the original as they think` – Corentin Limier Jan 07 '20 at 12:53
  • 1
    @CorentinLimier Ok, thank u for explanation. So resume: it's not a good practice and I can turn off that warning monit in Ide options without concerns. – martin Jan 07 '20 at 12:56

0 Answers0