英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论