estebanpdl estebanpdl - 6 months ago 59
Python Question

Sort Pandas DataFrame by value

I Know this question has a lot of answers, for example: How to sort pandas data frame using values from several columns?

I tried the solutions given by the users, but I cannot get a sorted column.

I built a DataFrame:

weekly = {'Tweet ID': Series(tweetID),
'Fecha de Publicación': Series(tweetDate),
'Tweet': Series(textStatus),
'Retweets': Series(retweetCount),
'Favoritos': Series(favoriteCount),
'Hashtags': Series(hashtags),
'Menciones': Series(mentions)}

weeklyAnalysis = DataFrame(weekly)


Then I call for specific tweets:

maxTweets = weeklyAnalysis[['Tweet', 'Retweets']]
[weeklyAnalysis['Retweets'] >= promedioRts]


This is what I tried, in order to sort retweets column:

maxTweets.sort_values(by=['Tweet', 'Retweets'], ascending=[False, True])


How can I actually sort that retweets column.

Any suggestions are appreciated! Thanks

Answer

If I'm understanding you correctly, you're trying to sort that df by 'retweets'? use:

maxTweets_sorted = maxTweets.sort_values(by='Retweets')