Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

python - Going through string columns and sort cell values in Pandas

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use this trick (unpacking a dataframe and using pd.DataFrame.assign):

df.assign(**df.select_dtypes(include='object').applymap(lambda x: '; '.join(sorted(x.split('; ')))))

Output:

                  col1     col2  col3
0          banana; cat  a; c; d     4
1  apple; kiwi; orange  c; p; u     1
2                melon     a; m     4

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...