Suppose we have the following dataframe:
d = {'col1':['cat; banana','kiwi; orange; apple','melon'],
'col2':['a; d; c','p; u; c','m; a'],
'col3':[4,1,4]}
df= pd.DataFrame(d)
for all the string columns I want to sort the values alphabetically, I know how to do this column by column, namely:
df['col1'] = df['col1'].map(lambda x: '; '.join(sorted(x.split('; '))))
and similarly for col2
I wonder how one can does this for the whole dataframe? I tried to select the string objects and do the map method, but it didn't work. Namely:
df.select_dtypes(include='object').map(lambda x: '; '.join(sorted(x.split('; '))))
Update: So an inefficient way of doing this would be:
v = df.select_dtypes(include='object').applymap(lambda x: '; '.join(sorted(x.split('; '))))
w = df.select_dtypes(exclude='object')
pd.concat([v, w], axis=1)
But I am sure there are better ways.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…