Namedtuples problem

Pavol Lisy pavol.lisy at gmail.com
Thu Feb 23 09:19:03 EST 2017


On 2/23/17, Peter Otten <__peter__ at web.de> wrote:
> Peter Otten wrote:
>
>> Functions to the rescue:
>
> On second thought this was still more code than necessary.

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

>>> import io  # I simlulate file in csv with header (like Deborah has)
>>> data = """\
what,location
foo,here
foo,there
bar,eberywhere"""

>>> df = pd.read_csv(io.StringIO(data))  # reading csv into DataFrame is very simple

>>> print(df[df.what == 'foo'])  # data where what == foo
  what location
0  foo     here
1  foo    there

>>> list(df[df.what=='foo'].location.values)  # list of values
['here', 'there']

>>> list(df.itertuples())  # if we want to use namedtuples
[Pandas(Index=0, what='foo', location='here'), Pandas(Index=1,
what='foo', location='there'), Pandas(Index=2, what='bar',
location='eberywhere')]

etc.



More information about the Python-list mailing list