I have a Pandas dataframe with a colum with some string and some NaN values.
df['column'] = ['a', 'b', NaN, 'c']
What I want to achieve is to transform the value in dicts, maintaining the original value:
df['column'] = [{'key': 'a'}, {'key': 'b'} , {'key': None}, {'key': 'c'}]
I successfully got it with an apply:
df['column'] = df['column'].apply(lambda value: {"value": None} if type(value) == pd._libs.missing.NAType or pd.isna(value) or pd.isnull(value) or value == '' else {"value": value})
But i'd like to reach the same result using the vectorized approach if possibile.
CodePudding user response:
Vectorization is not possible since you are using complex object types like dictionaries having said that here is a slightly more efficient and concise approach than apply
:
df['column'].replace(['', np.nan], None).map(lambda k: {'key': k})
Result
0 {'key': 'a'}
1 {'key': 'b'}
2 {'key': None}
3 {'key': 'c'}
Name: column, dtype: object