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

Rename None in a list under pandas column

Let’s say I have the following dataframe:

Value
[None, A, B, C]
[None]

I would like to replace None value in the column with none but it seems I couldn’t figure out it.

I used this but not working.

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

df['Value'] = df['Value'].str.replace('None','none')

>Solution :

None is a built-in type in Python, so if you want to make it lowercase, you have to convert it to a string.

There is no built-in way in Pandas to replace values in lists, but you can use explode to expand all the lists so that each individual item of each list gets its own row in the column, then replace, then group back together into the original list format:

df['Value'] = df['Value'].explode().replace({None: 'none'}).groupby(level=0).apply(list)

Output:

>>> df
             Value
0  [none, A, B, C]
1           [none]
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