Skip to content
Advertisement

Replacing None with a list within a dataframe

I have the below dataframe which comes from a JSON


0        [0, 5.9, 4]       [1, 6, 23]      [2, 6.2, 2]
1      [0, 48, 3.11]      [1, 50, 10]    [2, 55, 13.1]
2   [0, 1.42, 90.26]  [1, 1.43, 91.8]   [2, 1.44, 121]
3    [0, 970, 18.41]   [1, 990, 1.53]             None
4    [0, 970, 18.42]   [1, 990, 1.55]  [2, 1000, 22.5]
5     [0, 740, 9.37]   [1, 990, 1.53]             None
6     [0, 740, 9.37]   [1, 900, 2.21]   [2, 990, 1.55]
7    [0, 970, 18.45]    [1, 990, 1.6]             None
8     [0, 740, 9.39]   [1, 990, 2.55]             None
9     [0, 970, 18.4]    [1, 990, 1.6]             None
10      [0, 42, 1.1]    [1, 85, 1.91]    [2, 90, 1.04]

trying to format ready for db insertion, i am splitting using .tolist() but getting error for None entries.

tried fillna and replace to insert a dummy list i.e. [0,0,0] but will only let me replace with a string. Any suggestions welcome.

this works

#df_split_batl = df_split_batl.fillna(‘xx’) #df_split_batl = df_split_batl.replace(‘xx’,’yy’)

but these dont

#df_split_batl = df_split_batl.fillna([0,0,0])

#df_split_batl = df_split_batl.fillna(‘xx’)

#df_split_batl = df_split_batl.replace(‘xx’,[0,0,0])

Advertisement

Answer

Check the following link, it might be helpful for your case: Replace NaN with empty list in a pandas dataframe

Instead of replacing it with an empty list, you’ll replace it with a list containing elements.

RGS20 :)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement