Home > Net >  Get row from list with dictionary's by searching between dates and var
Get row from list with dictionary's by searching between dates and var

Time:10-19

So I've got this dict in python

taxes = [{  "kind": "g",  "vat": 21.0,  "start_date": "2013-01-01",  "end_date": "2013-12-31"},
{  "kind": "g",  "vat": 9.0,  "start_date": "2014-01-01",  "end_date": "2014-12-31"},
{  "kind": "e",   "vat": 21.0,   "start_date": "2013-01-01",  "end_date": "2013-12-31"},
{  "kind": "e",  "vat": 9.0,  "start_date": "2016-01-01",   "end_date": "2016-12-31"}]

I want to be able to get the row back where a date is between the start and end date and the kind is e or g

I tried to convert the dict to pandas dataframe, thinking that the search would be easier.

like this

mask = (taxes['start_date'] <= '2013-05-31') & (taxes['end_data'] >= '2013-05-31')
print(belastingen.loc[mask])

But that already gives an error.

Looping thru the list checking if the date is between start and end date and when kind is 'g' also does not look like a very pythonic solution.

So I wonder. Is pandas the right way to start? Or should I just dive into the dict and get the Information from the dict itself?

CodePudding user response:

Using pandas should work well:

df = pd.DataFrame(taxes)
out = df[(df['start_date'] <= '2013-05-31') & (df['end_date'] >= '2013-05-31')]

output:

  kind   vat  start_date    end_date
0    g  21.0  2013-01-01  2013-12-31
2    e  21.0  2013-01-01  2013-12-31

Back to dictionary:

d = out.to_dict('records')

output:

[{'kind': 'g', 'vat': 21.0, 'start_date': '2013-01-01', 'end_date': '2013-12-31'},
 {'kind': 'e', 'vat': 21.0, 'start_date': '2013-01-01', 'end_date': '2013-12-31'}]
  • Related