0

I have the following data frame.

df 

col_1    col_2     col_3
min1     max1     target1
min2     max2     target2

I would like to rename columns col_1, col_2, col_3 based on their value as Min, Max, and Target respectively. I would like to have something below as a result.

Min     Max     Target
min1    max1    target1
min2    max2    target2

Can anyone show me a simple trick to do this using Pandas python?

  • 1
    What do you mean by *"based on their value"* ? @Kapital – Vishnudev Krishnadas Mar 25 '20 at 09:34
  • 1
    Is not possible set `df.columns = ['Min','Max','Target']` ? – jezrael Mar 25 '20 at 09:37
  • For example, for col_0 it has value min1 and min2. Based on this min1 and min2 value rename the column as Min. Since it has those names under it. –  Mar 25 '20 at 09:37
  • @jezrael My Table is really big and I would like to create a rule to do that. In your case, it was simple. –  Mar 25 '20 at 09:38
  • @Kapital - OK, but how is possible distinguish what column is filled by minimal values, what column by maximal, what column is filled by target? Is possible explain more? – jezrael Mar 25 '20 at 09:40
  • What happens if `col_1` has `min1` and `max1`? – Vishnudev Krishnadas Mar 25 '20 at 09:41
  • @Vishnudev In my case, col_1 don't have both of them. But what if we create a rule? Something like if the data frame columns have the word ```min``` inside it rename the column as a "Min" whatever value inside it? –  Mar 25 '20 at 09:45
  • It's not a good practise to depend on the values for columns because it might cause inconsistency in further code. You won't be sure of the column names. Suppose there is a column with value `target1start1` what would you rename it as? `target` or `target1` or `targetstart`. – Vishnudev Krishnadas Mar 25 '20 at 09:49
  • @Kapital - Are you sure not exist 2 columns with `min` substring? – jezrael Mar 25 '20 at 09:50
  • @Vishnudev Is that possible to do some regular expression for that to extract the word target only whatever the combination of words we have? –  Mar 25 '20 at 09:52
  • 1
    @jezrael You said it is duplicate and you provided me a link on top of my post but my question and the solution you provided me are completely different. I saw that solution before I post this question. Could you please remove that? –  Mar 25 '20 at 10:01
  • So u wanted MAX of the column max. And likewise for the column min. – letdatado Apr 24 '22 at 01:00

1 Answers1

2

Well, I am not sure that you should rename columns this way, but here is a way you could do it. First remove numbers from string and then use the most common string.

from collections import Counter

cols = [Counter(df[col].str.replace('\d+', '')).most_common()[0][0].capitalize()
        for col in df.columns
        ]

df.columns = cols

Output:

    Min     Max     Target
0   min1    max1    target1
1   min2    max2    target2
above_c_level
  • 3,579
  • 3
  • 22
  • 37