How to .apply(lambda x: x.split()) on a partial dataframe

120

Question: How to .apply(lambda x: x.split()) on a partial dataframe

I am trying to replace this kind of answer in my data frame : case_1 case_2 case_3

by : [case_1,case_2,case_3]

.apply(lambda x: x.split()) seems to be a good way to do it

But I also have some Nan value in the dataframe that raise this error : enter image description here

Does somebody have an idea how should I proceed ? Maybe how to apply the function on the python error dataframe only if it has been answered ?

    r_bt.res_enquete_poteaux['type_anomalie'].apply(lambda x: x.split() if x!=np.nan)                                                                                     ^ SyntaxError: invalid syntax 

Total Answers: 1

97

Answers 1: of How to .apply(lambda x: x.split()) on a partial dataframe

Use Series.str.split working also with missing values:

r_bt.res_enquete_poteaux['type_anomalie'].str.split() 

Your solution is possible change with notna for test not missing values:

r_bt.res_enquete_poteaux['type_anomalie'].apply(lambda x: x.split() if pd.notna(x) else x)