Skip to content

Instantly share code, notes, and snippets.

@jottr
Last active July 16, 2016 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jottr/bc8f6452485c4423543dadf7f8cef9da to your computer and use it in GitHub Desktop.
Save jottr/bc8f6452485c4423543dadf7f8cef9da to your computer and use it in GitHub Desktop.
# A pandas dataframe with the following keys
df.keys()#=>
'''
Index([u'value', u'quantity', u'transaction.post_date',
u'transaction.currency.guid', u'transaction.currency.mnemonic',
u'account.fullname', u'account.commodity.guid',
u'account.commodity.mnemonic'],
dtype='object')
'''
# Let's see which kind of values 'account.fullname' holds
df['account.fullname'][11:20] # =>
'''
guid
13592a1b945f1ee334c5982049b35327 Aktiva:Barvermögen:Girokonto
9d362dd65015be27e5d9d9647c4d4f29 Aktiva:Barvermögen:Girokonto
5f0744ca60a81c338ae6ef24c1cb9156 Aufwendungen:Haushalt:Lebensmittel
45686c3148f09629606d57711417c788 Aufwendungen:Kleidung
6a4cf43b57252b85611b0e2356531d10 Aktiva:Barvermögen:Girokonto
4a7566ddd488015ff620367a8f30b870 Aktiva:Barvermögen:Girokonto
15d37609237adafa981d3b4ab4f0f24a Aktiva:Barvermögen:Girokonto
52667ab5d98ef99064127825f399e39e Aktiva:Barvermögen:Girokonto
5841a2b853c47f2f0afc1695c202c6d9 Aktiva:Barvermögen:Girokonto
Name: account.fullname, dtype: object
'''
# Ok, let's display some values greater than 100, which works...
print(df.loc[df['value'] > 100]).head(5) #=>
'''
value quantity transaction.post_date \
guid
b0645936e89d6309130008d6aa9816d5 356 356 2015-02-01 00:00:00+01:00
9fadd75dc87368bd229d963e13b13fa4 1986.59 1986.59 2015-03-01 00:00:00+01:00
5876b1729a08f4e05fc9924baad1980b 1120 1120 2015-03-25 00:00:00+01:00
6a4cf43b57252b85611b0e2356531d10 3482 3482 2015-03-26 00:00:00+01:00
a3724ae9806d6f6c20036af173ac3ec4 1200 1200 2015-03-27 00:00:00+01:00
'''
# Trying to filter for a certain account name, strangely this does not work. But why?
print(df['account.fullname' == 'Aufwendungen:Kleidung']) # =>
'''
KeyErrorTraceback (most recent call last)
<ipython-input-73-650e6d6b2f3d> in <module>()
----> 1 print(df['account.fullname' == 'Aufwendungen:Kleidung'])
/Users/jonas/dev/ipython/notebook/gnucash/.direnv/python-2.7.11/lib/python2.7/site-packages/pandas/core/frame.pyc in __getitem__(self, key)
1995 return self._getitem_multilevel(key)
1996 else:
-> 1997 return self._getitem_column(key)
1998
1999 def _getitem_column(self, key):
/Users/jonas/dev/ipython/notebook/gnucash/.direnv/python-2.7.11/lib/python2.7/site-packages/pandas/core/frame.pyc in _getitem_column(self, key)
2002 # get column
2003 if self.columns.is_unique:
-> 2004 return self._get_item_cache(key)
2005
2006 # duplicate columns & possible reduce dimensionality
/Users/jonas/dev/ipython/notebook/gnucash/.direnv/python-2.7.11/lib/python2.7/site-packages/pandas/core/generic.pyc in _get_item_cache(self, item)
1348 res = cache.get(item)
1349 if res is None:
-> 1350 values = self._data.get(item)
1351 res = self._box_item_values(item, values)
1352 cache[item] = res
/Users/jonas/dev/ipython/notebook/gnucash/.direnv/python-2.7.11/lib/python2.7/site-packages/pandas/core/internals.pyc in get(self, item, fastpath)
3288
3289 if not isnull(item):
-> 3290 loc = self.items.get_loc(item)
3291 else:
3292 indexer = np.arange(len(self.items))[isnull(self.items)]
/Users/jonas/dev/ipython/notebook/gnucash/.direnv/python-2.7.11/lib/python2.7/site-packages/pandas/indexes/base.pyc in get_loc(self, key, method, tolerance)
1945 return self._engine.get_loc(key)
1946 except KeyError:
-> 1947 return self._engine.get_loc(self._maybe_cast_indexer(key))
1948
1949 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)()
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)()
pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)()
pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)()
KeyError: False
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment