Question

I am trying to remove nan values from a final csv file, which will source data for digital signage.

I use fillna to remove empty data to keep 'nan' from appearing on the sign. fillna is working, but because this data is formatted, I get ()- in the empty csv fields.

df = pd.read_csv(filename)
    df = df.fillna('') 
    df = df.astype(str)
    df['PhoneNumber']=df['CONTACT PHONE NUMBER'].apply(lambda x: '('+x[:3]+')'+x[3:6]+'-'+x[6:10])

I tried writing an if...else statement to separate lines in the array; but since the formatting is applied to the entire list, not entry by entry, that doesn't work.

Was it helpful?

Solution

A simple modification to your lambda function should do the job:

>>> y=lambda x: (x and '('+x[:3]+')'+x[3:6]+'-'+x[6:10]) or ''
>>> y('123456789')
'(123)456-789'
>>> y('')
''

EDIT:

You could also replace the and/or idiom with if-else construct:

>>> y=lambda x: '('+x[:3]+')'+x[3:6]+'-'+x[6:10] if x else ''
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top