简体   繁体   English

如果字典的任何值与 pandas dataframe python 中的条件匹配,则获取数据

[英]Get data if any value of a dictionary matches a condition in pandas dataframe python

I want to return the value of q_start column if UPS_HEAD or UPS_DRV < 95如果 UPS_HEAD 或 UPS_DRV < 95,我想返回 q_start 列的值

I have the following Dataframe:我有以下 Dataframe:

rows_list = [{'q_end': '2022-06-24 15:00:00', 'q_start': '2022-06-24 15:59:59', 'summary': {'UPS_HEAD': 84, 'UPS_DRV': 84, 'ALLOW_AP': 18 }},
{'q_end': '2022-06-24 14:00:00', 'q_start': '2022-06-24 14:59:59', 'summary': {'UPS_HEAD': 95, 'UPS_DRV': 95, 'ALLOW_AP': 18 }},
{'q_end': '2022-06-24 13:00:00', 'q_start': '2022-06-24 13:59:59', 'summary': {'UPS_HEAD': 91, 'UPS_DRV': 91, 'ALLOW_AP': 18 }}]
df = pd.DataFrame(rows_list)

The output should be like: output 应该是这样的:

output = [2022-06-24 15:00:00, 2022-06-24 13:00:00]

Assuming "summary" is a Series of dictionaries, use the str accessor and boolean indexing :假设“summary”是一系列字典,使用str访问器和boolean 索引

m1 = df['summary'].str['UPS_HEAD'].lt(95)
m2 = df['summary'].str['UPS_DRV'].lt(95)

out = df.loc[m1|m2, 'q_start'].to_list()

If you have many more than 2 columns to check, a better approach might be:如果要检查的列多于 2 列,更好的方法可能是:

cols = ['UPS_HEAD', 'UPS_DRV']
m = pd.json_normalize(df['summary'])[cols].lt(95).any(1)

out = df.loc[m, 'q_start'].to_list()

output: ['2022-06-24 15:00:00', '2022-06-24 13:00:00'] output: ['2022-06-24 15:00:00', '2022-06-24 13:00:00']

暂无
暂无

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

相关问题 在行匹配条件的Pandas DataFrame中获取第一列值 - Get first column value in Pandas DataFrame where row matches condition 检查字典的任何值是否与条件匹配 - Check if any value of a dictionary matches a condition 在符合条件的 pandas 数据框中查找列名及其各自的值,并将结果存储在字典中 - Find column names and their respective value in a pandas data frame which matches a condition and store the result in a dictionary Python-通过索引条件从pandas数据框中获取价值 - Python - Get value from pandas dataframe by index condition Python:从对象字典中获取值(对象),其中一个对象的字段与值(或条件)匹配 - Python: Get values (objects) from a dictionary of objects in which one of the object's field matches a value (or condition) Python/Pandas:在 dataframe 或字典中查找最小值 - Python/Pandas: Find min value in dataframe or dictionary (python) 如何将字典值转换为 pandas DataFrame - (python) How to convert a dictionary value into a pandas DataFrame 在字典中查找与条件匹配的值 - Find value in the dictionary that matches the condition 如何将 pandas 数据框列与字典键与数据框索引匹配的字典值相乘 - How to multiply pandas dataframe columns with dictionary value where dictionary key matches dataframe index 对 DataFrame 执行操作并获得结果作为 Dataframe 匹配 Pandas 中另一个 DataFrame 中存在的任一条件 - Perform operation on DataFrame and get result as Dataframe which matches either of the condition present in another DataFrame in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM