0

I have the following code:

df1 = pd.read_excel(f, sheet_name=0, header=6)
# Drop Columns by position
df1 = df1.drop([df1.columns[5],df1.columns[8],df1.columns[10],df1.columns[14],df1.columns[15],df1.columns[16],df1.columns[17],df1.columns[18],df1.columns[19],df1.columns[21],df1.columns[22],df1.columns[23],df1.columns[24],df1.columns[25]], axis=1)

# rename cols

This is where I am struggling, as each time I attempt to rename the cols by position it returns "None" which is a <class 'NoneType'> ( when I use print(type(df1)) ). Note that df1 returns the dataframe as expected after dropping the columns

I get this with everything I have tried below:

column_indices = [0,1,2,3,4,5,6,7,8,9,10,11]
new_names = ['AWG Item Code','Description','UPC','PK','Size','Regular Case Cost','Unit Scan','AMAP','Case Bill Back','Monday Start Date','Sunday End Date','Net Unit']
old_names = df1.columns[column_indices]
df1 = df1.rename(columns=dict(zip(old_names, new_names)), inplace=True)

And with:

df1 = df1.rename({df1.columns[0]:"AWG Item Code",df1.columns[1]:"Description",df1.columns[2]:"UPC",df1.columns[3]:"PK",df1.columns[4]:"Size",df1.columns[5]:"Regular Case Cost",df1.columns[6]:"Unit Scan",df1.columns[7]:"AMAP",df1.columns[8]:"Case Bill Back",df1.columns[9]:"Monday Start Date",df1.columns[10]:"Sunday End Date",df1.columns[11]:"Net Unit"}, inplace = True)

When I remove the inplace=True essentially setting it to false, it returns the dataframe but without any of the changes I am wanting.

The tricky part is that in this program my column headers will change each time, but the columns the data is in will not. Otherwise I would just use df = df.rename(columns=["a":"newname"])

Image of my Dataframe in Python

Image of Dataframe in Excel

Xeltoid
  • 88
  • 8
  • 1
    Your [mre] should include an example DataFrame. [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Jul 26 '22 at 19:02
  • 1
    When using `inplace=True` - why do you assign the result back to `df1`? – wwii Jul 26 '22 at 19:03
  • @wwii not sure tbh, but it doesn't work if I do it or don't do it, same thing still happens. – Xeltoid Jul 26 '22 at 19:05
  • 1
    @wwii means that code like `df.rename(columns={df.columns[0]: 'first_col'}, inplace=True)` in and of itself will alter the first column name into "first_col"; you don't assign it *to* the df. Are you saying that `df1.rename({df1.columns[0]:"AWG Item Code", ...}, inplace=True)` *without* `df1 = ` in front of it, is not working? If so, what is the error that you get? – ouroboros1 Jul 26 '22 at 19:12
  • @ouroboros1 I was missing the .rename(columns={ part of that. Thank you. Working now – Xeltoid Jul 26 '22 at 19:15
  • Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type `ctrl-k`) … [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question) …[Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) ... [You should not post code as an image because:...](https://meta.stackoverflow.com/a/285557/2823755) – wwii Jul 26 '22 at 21:43

3 Answers3

1

One simpler version of your code could be :

df1.columns = new_names

It should work as intended, i.e. renaming columns in the index order.

Otherwise, in your own code : if you print df1.columns[column_indices]

You do not get a list but a pandas.core.indexes.base.Index

So to correct your code you just need to change the 2 last lines by :

old_names = df1.columns[column_indices].tolist()
df1.rename(columns=dict(zip(old_names, new_names)), inplace=True)

Have a nice day

0

I was dumb and missing columns=

df1.rename(columns={df1.columns[0]:"AWG Item Code",df1.columns[1]:"Description",df1.columns[2]:"UPC",df1.columns[3]:"PK",df1.columns[4]:"Size",df1.columns[5]:"Regular Case Cost",df1.columns[6]:"Unit Scan",df1.columns[7]:"AMAP",df1.columns[8]:"Case Bill Back",df1.columns[9]:"Monday Start Date",df1.columns[10]:"Sunday End Date",df1.columns[11]:"Net Unit"}, inplace = True)

works fine

Xeltoid
  • 88
  • 8
0

I am not sure whether this answers your question:

There is a simple way to rename the columns:

If I have a data frame: say df1. I can see the columns name using the following code:

df.columns.to_list()

which gives me suppose following columns name:

['A', 'B', 'C','D']

And I want to keep the first three columns and rename them as 'E', 'F' and 'G' respectively. The following code gives me the desired outcome:

df = df[['A','B','C']]
df.columns = ['E','F','G]

new outcome:

df.columns.to_list()
output: ['E','F','G']
Jui Sen
  • 377
  • 3
  • 12
  • Not 100% sure in all honestly, I just needed code to take column names each week (that changed slightly each week, so I couldn't just do it by name in my code directly) and rename them and drop others based on their location. I did eventually figure out that my original code worked, just was missing 'columns='. just a dumb mistake on my part. – Xeltoid Jul 28 '22 at 14:38