Dataframe iterating question : 3 ways of calling a row and column

Rick Johnson rantingrickjohnson at gmail.com
Mon Aug 21 19:41:09 EDT 2017


On Monday, August 21, 2017 at 12:08:54 PM UTC-5, zach.... at orthofi.com wrote:
> I wouldn't say I'm a Python noob, but I wouldn't say I'm a Python expert either. I work in data science and use Pandas Dataframes a lot. My question is regarding the difference in calling out a specific row, column combination in a dataframe.
>
> I see 3 ways of doing this:
> (1) df.loc[row_ind, column_ind]
> (2) df.column_ind.loc[row_ind]
> (3) df[column_ind].loc[row_ind]
>
> where column_ind is the column name & row_ind is the named row index/row name in the dataframe.
>
> Can anyone enlighten me as to the differences between the above 3 methods of getting to the same cell in the dataframe?
> Are there speed differences?
> Is it simply a preference thing?
> Is there a PEP8 preferred way of doing this?

If you have not read PEP8 in it's entirety, then i encourage
you to do so. Of course, i'm not suggesting you must follow
PEP8 _religiously_ (we have a formal Inquisition court for
that), but you should at least be aware of Python idioms,
and try to incorporate as many of them as you can tolerate,
so that when you are called to testify, perhaps you can
escape the worst of the punishments. Personally, i'm
somewhere around 85% in agreement with the suggestions of
PEP8, and that's reasonable and tolerable enough...

However, the Zen is a whole different beast!

You _must_ be 100% in agreement with the Zen, and if not,
then your Python interpreter will "know", and will behave
mischievously as a punishment. GvR has installed a secret
_loki module, you see, specifically for this purpose. There
is also a _bigbrother module, but we're not allowed to talk
about bigbrother, as directed by the first and second rule
of pyclub.

--
>>> import this






More information about the Python-list mailing list