Use sort_values ascending for value greater than 100.

huangapple go评论59阅读模式
英文:

Use sort_values ascending for value greater than 100

问题

df2 = df[df['site stock'] > 100].sort_values(by='site stock', ascending=False).head(50)
英文:

My current code is:

df2 = df.loc[df['site stock'] >100].head(50)

df2.sort_values(by=['site stock'],ascending = False)

Is there a way to put this code in to just one line of output. I'm sure there is just can't figure out the order it needs to go in.

答案1

得分: 3

只需链接命令:

df2 = df.loc[df['site stock'] > 100].head(50).sort_values(by=['site stock'], ascending=False)

为了更好的可读性,你可以使用括号将其拆分为多行:

df2 = (df.loc[df['site stock'] > 100]
         .head(50)
         .sort_values(by=['site stock'], ascending=False)
      )

这还可以方便地快速注释掉某一行,以便在需要时进行测试:

df2 = (df.loc[df['site stock'] > 100]
         #.head(50)
         .sort_values(by=['site stock'], ascending=False)
      )
英文:

Just chain the commands:

df2 = df.loc[df['site stock'] > 100].head(50).sort_values(by=['site stock'], ascending = False)

For better readibility, you can split on several lines with parentheses:

df2 = (df.loc[df['site stock'] > 100]
         .head(50)
         .sort_values(by=['site stock'], ascending = False)
      )

It also makes it easy to quickly comment a line if needed to test something:

df2 = (df.loc[df['site stock'] > 100]
         #.head(50)
         .sort_values(by=['site stock'], ascending = False)
      )

答案2

得分: 2

你可以用点号将这两行连接在一起:

out = df.loc[df['site stock'] > 100].head(50).sort_values(by=['site stock'], ascending=False)

英文:

You can chain both lines together with dot:

out = df.loc[df['site stock']>100].head(50).sort_values(by=['site stock'],ascending = False)

huangapple
  • 本文由 发表于 2023年4月4日 17:55:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定