I have created the following pandas dataframe:
import pandas as pd
import numpy as np
ds = {'col1' :
['1489900119000',
'้้คค,1']
}
df = pd.DataFrame(data=ds)
which looks like this:
col1
0 1489900119000
1 ้้คค,1
I am trying to build a new column (called col2) which contains the values in col1 arranged as list. Hence the code:
df['col2'] = [list(map(int, str(x))) for x in df.col1]
Since there is that unexpected / invalid value at row 1 (คค,1), the code fails.
Is there any way to by-pass that row and fill it in with a default value (e.g. [9,9])? For example, the resulting dataframe would look like this:
>Solution :
You can use a conditional with str.isdigit:
df['col2'] = [list(map(int, str(x))) if x.isdigit() else [9,9] for x in df.col1]
Or with a custom function and try/except:
def digitize(s):
try:
return [int(x) for x in s]
except ValueError:
return [9, 9]
df['col2'] = df['col1'].apply(digitize)
Output:
col1 col2
0 1489900119000 [1, 4, 8, 9, 9, 0, 0, 1, 1, 9, 0, 0, 0]
1 ้้คค,1 [9, 9]
