0

I have a dataframe df

    a    b    c
0   5    6    9
1   6    7   10
2   7    8   11
3   8    9   12

So if I want to select only col a and b and store it in another df I would use something like this

df1 = df[['a','b']]

But I have seen places where people write it this way

df1 = df[['a','b']].copy()

Can anyone let me know what is .copy() because the earlier code works just fine.

Sai Kumar
  • 665
  • 2
  • 9
  • 21

1 Answers1

0

For example, if you want to rename a dataframe (example using replace):

df2=df
df2=df2.replace('blah','foo')

Here:

df==df2

Will be:

True

You want it to only do to, df2:

df2=df.copy()
df2=df2.replace('blah','foo')

Then now:

df==df2

Returns:

False
U13-Forward
  • 69,221
  • 14
  • 89
  • 114