python - Sort dataframe by length of a string column -


this question has answer here:

using python. have dataframe 3 columns:

author | title | reviews 

i want sort length of string in reviews column.

if

df.sort_values('review', ascending = false) 

it sorts alphabetically, starting 'z'.

how sort length of string in reviews column?

i think need len lengths assign index, sort_index , last reset_index:

df = pd.dataframe({'author':list('abcdef'),                    'title ':list('abcdef'),                    'review':['aa', 'aasdd', 'dwd','dswee dass', 'a', 'sds']})  print (df)   author      review title  0               aa      1      b       aasdd      b 2      c         dwd      c 3      d  dswee dass      d 4      e                e 5      f         sds      f  df.index = df['review'].str.len() df = df.sort_index(ascending=false).reset_index(drop=true) print (df)   author      review title  0      d  dswee dass      d 1      b       aasdd      b 2      c         dwd      c 3      f         sds      f 4               aa      5      e                e 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -