Home > Enterprise >  How to prevent NaN when using str.lower in Python?
How to prevent NaN when using str.lower in Python?

Time:04-18

I'm looking to convert a column to lower case. The issue is there are some instances where the string within the column only contains numbers. In my real life case this is due to poor data entry. Instead of having these values converted to NaN, I would like to keep the numeric string as is. What is the best approach to achieving this?

Below is my current code and output

import pandas as pd

df = pd.DataFrame({'col':['G5051', 'G5052', 5053, 'G5054']})

df['col'].str.lower()

Current Output

enter image description here

Desired Output

enter image description here

CodePudding user response:

Just convert to column to strings first:

import pandas as pd
df = pd.DataFrame({'col':['G5051', 'G5052', 5053, 'G5054']})
print(df['col'].astype(str).str.lower())

CodePudding user response:

Pre-Define the data as str format.

import pandas as pd
df = pd.DataFrame({'col':['G5051', 'G5052', 5053, 'G5054']}, dtype=str)
print(df['col'].str.lower())

CodePudding user response:

to add a slight variation to Tim Roberts' solution without using the .str accessor:

import pandas as pd
df = pd.DataFrame({'col':['G5051', 'G5052', 5053, 'G5054']})
print(df['col'].astype(str).apply(lambda x: x.lower()))
  • Related