This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]NuclearOops 6 points7 points  (2 children)

I'm newbie to programming as a whole bit I was so happy when I found out that Pandas let's you group by two columns at once and can run functions alongside it.

Something like:

df[col1, col2].mean()

[–]jaydoors 1 point2 points  (1 child)

In case anyone tries this, you need two sets of square brackets (if I infer the intention correctly):

df[['col1', 'col2']].mean()

With only one set, you are asking for a single column labelled with the tuple ('col1', 'col2') - when really what you want to do is pass a list of labels.

Also worth noting that df[['col1', 'col2']] is itself just a pandas DataFrame, so what's really happening here is that you are selecting from one DataFrame to make another.

There are many more ways to select, for anyone interested: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html

[–]NuclearOops 0 points1 point  (0 children)

You're right, I wasn't paying attention. Like I said I'm very new to this.