Namedtuples problem

Peter Otten __peter__ at web.de
Thu Feb 23 10:19:00 EST 2017


Pavol Lisy wrote:

> If we are talking about less code than necessary then probably we
> could use something from python's ecosystem...
> 
>>>> import pandas as pd  # really useful package

Yes, pandas looks like the perfect fit here.

There is a learning curve, though, so that it will probably pay off only 
after the second project ;) 

For example here's what my session might have looked like:

>>> df = pd.read_csv("what_location.csv")
>>> df
  what    location
0  foo        here
1  foo       there
2  bar  eberywhere

[3 rows x 2 columns]
>>> df.groupby("what")
<pandas.core.groupby.DataFrameGroupBy object at 0x7f52a71f3e48>
>>> print(df.groupby("what"))
<pandas.core.groupby.DataFrameGroupBy object at 0x7f52a71f3f60>
>>> df.groupby("what")["foo"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/pandas/core/groupby.py", line 2477, 
in __getitem__
    raise KeyError(str(key))
KeyError: 'foo'
>>> df.groupby("what").keys()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> df.groupby("what").keys
'what'

Frustrating.

PS:
>>> g.groups
{'foo': [0, 1], 'bar': [2]}





More information about the Python-list mailing list