1

Quite shortly: I have this DataFrame Dataframe

In the dataframe I have some duplicated columns with different values. How can I fix it so these have different column-names?

df_temporary.rename(columns={df_temporary.columns[3]: "OeFG%"}, inplace=True)
df_temporary.rename(columns={df_temporary.columns[11]:"DeFG%"}, inplace=True)
df_temporary.rename(columns={df_temporary.columns[5]: "OTOV%"}, inplace=True)
df_temporary.rename(columns={df_temporary.columns[13]: "DTOV%"}, inplace=True)
df_temporary.rename(columns={df_temporary.columns[8]: "OFT/FGA"}, inplace=True)
df_temporary.rename(columns={df_temporary.columns[-1]: "DFT/FGA"}, inplace=True)

df_temporary

I attempted using rename, first with all in one columns in one {}, and then hardcoding each of them afterwards; both resulting in the same result.

What I tried

I also tried following this: Renaming columns in a Pandas dataframe with duplicate column names?

Not much success implementing these in my code however.

Joak222
  • 13
  • 2

1 Answers1

2

Welcome to Stackoverflow :)

Great that you showed us a code sample of what you tried, and that you even linked to a stackoverflow post showing that you researched this problem!

In the future, try to avoid posting screenshots of tables and paste them in as text so that reviewers/helpers can easily copy your stuff to run some code of their own.

Now, for your problem: you can easily just change them column names by changing the value of df.columns. A mini-example (I was too lazy to make all the columns, and I'm not sure whether the rename is really what you wanted) for your case would be the following:

import pandas as pd

data = [["Boston Celtics", 51, 31, 0.542, 0.502],
        ["Phoenix Suns", 64, 18, 0.549, 0.510]]

df = pd.DataFrame(data, columns=['Team', 'W', 'L', 'eFG%', 'eFG%'])
df
             Team   W   L   eFG%   eFG%                                                                                                
0  Boston Celtics  51  31  0.542  0.502                                                                                                
1    Phoenix Suns  64  18  0.549  0.510

df.columns = ["Team", "W", "L", "eFG%", "OeFG%"]
df
             Team   W   L   eFG%  OeFG%                                                                                                
0  Boston Celtics  51  31  0.542  0.502                                                                                                
1    Phoenix Suns  64  18  0.549  0.510

As you can see, that final column has been renamed to OeFG% instead of eFG%. You can of course do this for any columns you like!

Hope this helps :)

Koedlt
  • 4,286
  • 8
  • 15
  • 33