Home > Software design >  Replace elements of a pandas series with a list containing a single string
Replace elements of a pandas series with a list containing a single string

Time:06-02

I am trying to replace the empty list in a pandas serie with a list containing a single string. Here is what I have:

a = pd.Series([ 
    [],
    [],
    ['a'],
    ['a'],
    ["a","b"]
])

The desired output is as following :

b = pd.Series([ 
    ['INCOMPLETE'],
    ['INCOMPLETE'],
    ['1'],
    ['1'],
    ["1","2"]
])

Where I try to replace the empty lists using boolean indexing, I get an automatic coercion of my list of a unique string to just string string:

a[a.map(len) == 0 ] = ['INCOMPLETE']
0    INCOMPLETE
1    INCOMPLETE
2           [a]
3           [a]
4        [a, b]

In contrast the manual replacement works a[0] = ['INCOMPLETE']

Does anyone have a workaround ? Thanks.

CodePudding user response:

You can't easily assign a list in pandas (pandas is not made to work with lists as items), you need to loop here:

b = pd.Series([x if x else ['INCOMPLETE'] for x in a], index=a.index)

output:

0    [INCOMPLETE]
1    [INCOMPLETE]
2             [a]
3             [a]
4          [a, b]
dtype: object

CodePudding user response:

Use lambda function with if-else for replace empty string, because if comapre are processing like False:

a = a.apply(lambda x: x if x else ['INCOMPLETE'])
print (a)
0    [INCOMPLETE]
1    [INCOMPLETE]
2             [a]
3             [a]
4          [a, b]
dtype: object

CodePudding user response:

import pandas as pd

a = pd.Series([ 
    [],
    [],
    ['a'],
    ['a'],
    ["a","b"]
])

convert_num = lambda x:list(map(lambda y:ord(y)-ord('a') 1,x))
map_data = lambda x:'Incomplete' if x==[] else convert_num(x) 
a = a.apply(map_data)
0    Incomplete
1    Incomplete
2           [1]
3           [1]
4        [1, 2]
dtype: object
  • Related