简体   繁体   English

根据约束从 Pandas DataFrame 中选择行

[英]Selecting row from a Pandas DataFrame based on constraints

I have several datasets that I import as csv files and display them in a DataFrame in Pandas. The csv files are info about Covid updates.我有几个数据集,我导入为 csv 文件并将它们显示在 Pandas 中的 DataFrame 中。csv 文件是关于 Covid 更新的信息。

The datasets has several columns relating to this, for example "country_region", "last_update" & "confirmed".数据集有几个与此相关的列,例如“country_region”、“last_update”和“confirmed”。

Let's say I wanted to look up the confirmed cases of Covid for Germany.假设我想查找德国的 Covid 确诊病例。

I'm trying to write a function that will return a slice of the DataFrame that corresponds to those constraints to be able to display the match I'm looking for.我正在尝试编写一个 function,它将返回 DataFrame 的一部分,它对应于这些约束,以便能够显示我正在寻找的匹配项。

I need to do this in some generic way so I can provide any value from any column.我需要以某种通用方式执行此操作,以便我可以提供来自任何列的任何值。

I wish I had some code to include but I'm stuck on how to even proceed.我希望我有一些代码可以包含,但我仍然不知道如何继续。 Everything I find online only specifies for looking up values relating to a pre-defined value.我在网上找到的所有内容都只指定查找与预定义值相关的值。

Something like this?是这样的吗?

def filter(county_region_val, last_update_val, confirmed_val, df):
    df = df.loc[((df['county_region'] == county_region_val) & (df['last_update'] == last_update_val) & (df[''confirmed'] == confirmed_val)).reset_index(drop=True)
    return df

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

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