根据存储在数组中的值过滤字典

时间:2018-02-25 20:09:07

标签: python dictionary

我有以下格式的字典:

data={'city':np.array(['chicago','champaign','kansas city','dallas','champaign','chicago']),
  'state':np.array(['IL','IL','KA','TX','IL','IL']), 'transaction_dt':np.array(['1-1-2018','2-1-2018','3-1-2018','7-1-2018','1-1-2018','2-1-2018'])}

我想获得状态IL中发生的所有事务的transaction_dt。我怎么能在字典里这样做? (我知道如何在pd数据框中执行此操作,但字典是如此不同!!)

2 个答案:

答案 0 :(得分:1)

您可以使用布尔掩码:

>>> res
array(['1-1-2018', '2-1-2018', '1-1-2018', '2-1-2018'],
      dtype='<U8')

输出:

 real function f1(et(1),et(2),t)

答案 1 :(得分:0)

我省略了这是In [1]: data = { ...: 'city':['chicago','champaign','kansas city','dallas','champaign','chicago'], ...: 'state':['IL','IL','KA','TX','IL','IL'], ...: 'transaction_dt':['1-1-2018','2-1-2018','3-1-2018','7-1-2018','1-1-2018','2-1-2018'] ...: } In [2]: desired_transactions = [transaction_dl ...: for state, transaction_dl in zip(data['state'], data['transaction_dt']) ...: if state == 'IL'] ...: In [3]: desired_transactions Out[3]: ['1-1-2018', '2-1-2018', '1-1-2018', '2-1-2018'] ,但它们的工作方式相同

smoke.xml