按值对 Pandas DataFrame 进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37287938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort Pandas DataFrame by value
提问by estebanpdl
I Know this question has a lot of answers, for example: How to sort pandas data frame using values from several columns?
我知道这个问题有很多答案,例如:如何使用多列中的值对Pandas数据框进行排序?
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 retweetscolumn.
我怎样才能真正对转推列进行排序。
Any suggestions are appreciated! Thanks
任何建议表示赞赏!谢谢
回答by Razzi Abuissa
By default pandas does not sort in place, unlike Python's list
.
默认情况下,pandas 不会就地排序,这与 Python 的list
.
Change
改变
maxTweets.sort_values(by=['Tweet', 'Retweets'], ascending=[False, True])
to
到
maxTweets = maxTweets.sort_values(by=['Tweet', 'Retweets'], ascending=[False, True])
or pass inplace=True
或通过 inplace=True
maxTweets.sort_values(by=['Tweet', 'Retweets'], ascending=[False, True], inplace=True)
回答by David Schuler
If I'm understanding you correctly, you're trying to sort that df by 'retweets'? use:
如果我对您的理解正确,您是在尝试通过“转推”对 df 进行排序吗?用:
maxTweets_sorted = maxTweets.sort_values(by='Retweets')