4

My attempt: df['uid'] = df.uid.astype(int)

Which works...! However, Python doesn't like it:

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

My question - what's the "best practice" of how to do this simple code?

Research so far:

Pandas: change data type of columns

A value is trying to be set on a copy of a slice from a DataFrame Warning

Attempts:

  1. df[df['uid']].astype(int)

...some of the links say to use iloc but I can't see how that can be used here...

Community
  • 1
  • 1
jhub1
  • 611
  • 3
  • 7
  • 19
  • It's not the `astype(int)` It's the way you constructed `df` prior. – piRSquared Apr 12 '17 at 22:57
  • I've read the other pages - I'm still having trouble reproducing their solutions aren't exactly what I'm trying to do (I think?). One method is to make a copy, but I'm trying to avoid making a copy and do it in place since the dataframe I have is huge. – jhub1 Apr 12 '17 at 23:17
  • Can you clarify by what you mean by df prior? I'm new at this. Thanks! @piRSquared – jhub1 Apr 12 '17 at 23:18
  • `df['uid'] = df.uid.astype(int)` implies `df` already existed. It was how it was constructed that left it as a `copy`. – piRSquared Apr 13 '17 at 00:27
  • 1
    If you want to do this using loc, you would do it this way : `df.loc[:, 'uid'] = df['uid'].astype(int)` – dmdip Apr 13 '17 at 05:31
  • @dmdip wow that was exactly what i was looking for. so in plane english - is that "out of all columns, select UID column and then astype that column of the ORIGINAL DF." right? – jhub1 Apr 13 '17 at 22:32
  • @jhub1 : Your statement suggests that the .loc merely changed the type of the column to int. I am not sure if pandas recognises that this is all that has to be done, or whether it creates a copy of that column (in memory), changes that copy to int, and then copies it back to replace the existing uid column. – dmdip Apr 14 '17 at 06:25

0 Answers0