0

As I understand it, when I'm changing a column name, the decision to set inplace to true or false depends on whether I want to mutate the original dataframe (inplace=True) or create a new dataframe object (inplace=False).

I have a source file in xlsx where the column names I want for my dataframe are in row 2. I need to change one of the column names so it conforms with the rest of the analysis.

df_original = pd.read_excel('filepath.xlsx', sheet_name = "LookUpReferences", skiprows = 1)
df_original = df_original.rename(columns = {'WrongName': 'CorrectName'}, inplace = True)

When I do this and, for example, look at output via the head() function, I get

AttributeError: 'NoneType' object has no attribute 'head'

I can resolve this by creating a new dataframe and setting inplace=False, i.e.

df_new = df_original.rename(columns = {'WrongName': 'CorrectName'}, inplace = False)

But this will get cumbersome. So I'm trying to understand why the inplace=True isn't working the way I want it to. Is it the skiprows portion of the read_excel() ??? That's the only reason I can think of why I'm getting a NoneType. But, very much like Jon Snow, I know nothing...

Thanks!

FBruzzesi
  • 6,385
  • 3
  • 15
  • 37
harrison10001
  • 109
  • 1
  • 6

1 Answers1

0

As you said yourself, inplace=True mutates the original dataframe, thus you don't have to re-assign it. On the other hand, the default setting is inplace=False, thus you can rename (and re-assign ) as:

df_new = df_original.rename(columns = {'WrongName': 'CorrectName'})

I am not sure if this is also too cumbersome for you.

Moreover as explained here inplace performance, there is no guarantee that an inplace operation runs faster.

FBruzzesi
  • 6,385
  • 3
  • 15
  • 37
  • Thank you for your response! I suppose I'm trying to figure out why I can't use head() after I have renamed a column and set inplace = True – harrison10001 Jan 14 '20 at 11:28