简体   繁体   中英

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

I have the following 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 = [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 :

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:

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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