OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Distinguish repeating column names by adding an integer using pandas

  • Thread starter Thread starter Lynn
  • Start date Start date
L

Lynn

Guest
I have some columns that have the same names. I would like to add a 1 to the repeating column names

Data

Code:
Date        Type    hi  hello   stat    hi  hello   
1/1/2022    a       0   0       1       1   0

Desired

Code:
Date        Type    hi  hello   stat    hi1     hello1  
1/1/2022    a       0   0       1       1       0

Doing

Code:
mask = df['col2'].duplicated(keep=False)

I believe I can utilize mask, but not sure how to efficiently achieve this without calling out the actual column. I would like to call the full dataset and allow the algorithm to update the dupe.

Any suggestion is appreciated
<p>I have some columns that have the same names. I would like to add a 1 to the repeating column names</p>
<p><strong>Data</strong></p>
<pre><code>Date Type hi hello stat hi hello
1/1/2022 a 0 0 1 1 0
</code></pre>
<p><strong>Desired</strong></p>
<pre><code>Date Type hi hello stat hi1 hello1
1/1/2022 a 0 0 1 1 0
</code></pre>
<p><strong>Doing</strong></p>
<pre><code>mask = df['col2'].duplicated(keep=False)
</code></pre>
<p>I believe I can utilize mask, but not sure how to efficiently achieve this without calling out the actual column. I would like to call the full dataset and allow the algorithm to update the dupe.</p>
<p>Any suggestion is appreciated</p>
 
Top