Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: Renaming headers with identical names

I have several columns named the same (or not named at all, to be specific) in a dataframe. I need to rename them separately but df.rename method renames them altogether. For example, in a following df:

#     nan    nan    a    nan    nan    b    nan    nan
#     1       2     3     4      5     6     7      8
#     9      10     11    12    13     14    15     16

Following code changes all ‘nan’ headers into ‘word’:

df = df.rename(columns={df.columns[1]:'word'})

#   word    word    a    word   word    b   word   word
#     1       2     3     4      5     6     7      8
#     9      10     11    12    13     14    15     16

How do I make it so that I can change the header names separately?
The ultimate goal for this example is to make the header look like the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#    nan     nan    a     a      a     b     b      b
#     1       2     3     4      5     6     7      8
#     9      10     11    12    13     14    15     16

Update: manual assignment of header values won’t work in this case because this is a simplified version of my problem. I wouldn’t know what the header values are and how many of them there are.

Following is a for loop that was supposed to incorporate the renaming method:

word = 'nan'
for i in range (0, len(list(df))-1):
if str(list(df)[i]) != 'nan':
    word = str(list(df)[i])
df.rename(columns={df.columns[i]:word}) 

>Solution :

df.columns = pd.Series(df.columns).mask(lambda x:x=='nan').ffill()
df

   NaN  NaN   a   a   a   b   b   b
0    1    2   3   4   5   6   7   8
1    9   10  11  12  13  14  15  16
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading