1

I'm kinda new to pandas so if its a really simple question im sorry, but i couldnt find solution to a problem exactly like this. I have a dataframe with 2 columns with same name and i need to take 2nd one and rename it and place in different spot. But when i try anything, it affects both of those columns.

I tried renaming one of them with df.rename(columns=..) and with just for loop, but it always renames 2nd one too. Maybe i can rename 1 of them or pop 1st one and place it back later?

Dataframe

I have dataframe like this and i need 2 column names "Материал" renamed and put into different palce in that df (i already have index for it). And for 1st column i just need to skip it, i dont really care about its position for now.

I get the df from folder with 30+ files, each of them 20+ columns and 100k+ rows in same format so i cant change it in file.

Legaman
  • 11
  • 3
  • show us your dataframe, the goal, and the dataframe you are getting could help us understand your question – jjk Aug 22 '23 at 19:48
  • Most likely the way you get your dataframe from is how the problem can be solved. Can you provide your data or a reproducible example? – JustLearning Aug 22 '23 at 19:50
  • Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Aug 22 '23 at 19:55
  • Depends on how your dataframe looks like but you can also rename using something like this ```df.columns = ["xyz", "abc"]``` – Suraj Shourie Aug 22 '23 at 19:56

2 Answers2

0

Here is one approach with groupby.cumcount to deduplicate:

df = pd.DataFrame([[1,2,3,4]],
                  columns=list('ABAC')
                  )

c = df.columns.to_series()
s = c.groupby(c).cumcount()
df.columns = c.mask(c.eq('A') & s.eq(1), 'a')

Output:

   A  B  a  C
0  1  2  3  4

Or using a custom function if you want to handle several values:

def cust_rename(df, dic):
    c = df.columns.to_series()
    s = c.groupby(c).cumcount()
    return df.set_axis(
        [dic.get(t, t[0]) for t in zip(c, s)],
        axis=1)

out = cust_rename(df, {('A', 1): 'X', ('B', 0): 'Y'})

Output:

   A  Y  X  C
0  1  2  3  4
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Omg this one worked really well. Couldnt find anything like that on renaming columns. Thanks a lot! – Legaman Aug 22 '23 at 20:13
0
rename_data = pd.DataFrame()
rename_data.rename(columns=rename_columns, inplace=True)

rename_columns ={ a: 1 } look like.

when you run this, it renames the columns of the rename_data DataFrame according to the mapping specified in the rename_columns dictionary, and the changes are made directly to the rename_data DataFrame itself.

Hard
  • 105
  • 5