简体   繁体   English

Pandas - 如何根据另一列的条件找到 1 列的前 n 个元素

[英]Pandas - How do you find the top n elements of 1 column based on a condition from another column

I am struggling with a question based on Pandas. I have an earthquake data set with columns of countries and magnitudes.我正在努力解决基于 Pandas 的问题。我有一个包含国家和震级列的地震数据集。 I am asked to: "Find the top 10 states / countries where the strongest and weakest earthquakes occurred."我被要求:“找到发生最强和最弱地震的前 10 个州/国家。”

From this question, I garnered that I am meant to find the top 10 countries ["country"] with the highest values (value_counts), but sorting by magnitude ["mag"].从这个问题中,我得出结论,我打算找到具有最高值 (value_counts) 的前 10 个国家 ["country"],但按幅度 ["mag"] 排序。

How would I go about doing this?我将如何 go 这样做? I've looked around but there's nothing I've found about this online.我环顾四周,但我在网上找不到任何相关信息。

Are you sure you did not find something useful?你确定你没有找到有用的东西吗? If I understand your question correct, it is a simple one.如果我理解你的问题是正确的,这是一个简单的问题。 After creating a dataframe by using below methods, you will get what you need.使用以下方法创建 dataframe 后,您将获得所需的内容。

import pandas as pd 
df = pd.read_csv(".csv") 
df.nlargest(x, ['Column Name'])

x is the number of elements which are the largest ones. x 是最大元素的数量。 Same is goes for nsmallest too. nsmallest 也是如此。 Just use these:只需使用这些:

DataFrame.nsmallest(n, columns, keep='first')
DataFrame.nlargest(n, columns, keep='first')

Please read and check the documentation first.请先阅读并检查文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM