The main function I miss in Mathematica after reading a Python Pandas Book

Yesterday I finished Python for Data Analysis, 2nd Edition. A very nice book by Wes McKinney, the creator of panda’s library for Python, that explores basic data manipulation using Python.

For me (a Mathematica enthusiast), it was a great experience to see how data manipulation is handled in other languages. I love the elegance of the Mathematica environment with It clean and sharp syntax, and yes, the plots are incomparable more beautiful, but at the same time, that are some interesting learnings with pandas.

Going straight to the point, if there is a main function from pandas that I miss in Mathematica, It’s the pivot_table function. See how simple is the syntax using python:

As you can see, it’s as simple as dataset.pivot_table(row, columns, value, total option)

I remember this very old post from Stack Exchange from 2011 about this case. IMHO, pivot is a very basic core operation for data manipulation. It would be nice to have an implementation in some future Mathematica versions.

PS: Dataset can pivot very simple cases, but it has an uncontrollable behavior. It’s changes if all the second-level association have the same keys or not, and you can’t choose witch rows and columns you want to Pivot. For example:

titanic = ExampleData[{"Dataset", "Titanic"}]    
pivot = titanic[GroupBy["sex"], GroupBy["class"], N@*Mean, "age"]

Will return us:

That’s cool, but if I simulate a missing value in the dataset:

titanic2 = titanic[Select[!(#class ==  "1st" && #sex == "female")&]];
pivot2 = titanic2[GroupBy["sex"], GroupBy["class"], N@*Mean, "age"]

We get the unpivoted form:

To return the desired result, I need to do something like this:

pivot3 = Normal@pivot2;
pivot3[[All]] = KeyUnion[Values@pivot3, 0 &];
Dataset@pivot3

What I believe is very unintuitive, and don’t solve the case with more Columns and Rows.

UPDATE - strange result

If I remove N from:

titanic2[GroupBy["sex"], GroupBy["class"], N@*Mean, "age"]

to:

titanic2[GroupBy["sex"], GroupBy["class"], Mean, "age"]

We get:

I tried another functions in the 3rd argument, and I changes the pivot form.

5 Likes

I completely agree!

1 Like

Once the data is ‘ragged’ (which you did by doing a key drop) there is no ‘uniform’ way of doing a pivot I think. Note also that the Python example is not ragged, it just has NaN (i.e. Missing[] equivalent), so it is not ragged then… I’m thinking that GroupBy (or GatherBy for non-dataset) can achieve this, but I’m not familiar enough by the Python implementation to copy it in Mathematica.

2 Likes

Hi Sander. I updated the way I simulated the ragged case, I believe that now is more realistic.

Many categories of programming languages are found across the history of computer instruction, including chipset (assembler), list processing, structured, typed, untyped, relational, and objected oriented. These should not be confused with execution environments (e.g. single program compilers, runtime/interactive compilers) nor with integrated development environments (IDE’s).

Python and Wolfram Language are examples of computer instruction languages. Mathematica is an IDE. In my opinion comparing the two is non-sequitur.

Hi Richard, I could have used Wolfram Language in the topic, but paraphrasing Fermat with some poetic licence: “the topic character limit is too short to contain it”. So, I used Mathematica.

Load DataReshape.m

This can be done with the package “DataReshape.m”:

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/DataReshape.m"]

(It is in my TODO list to submit a few Wolfram Function Repository entries based on that package.)

Titanic data

In order to convert to wide form with ToWideForm we specify the ID column (“sex”), the Variable column (“class”), the Value column (“age”), and the aggregation function (Mean):

ToWideForm[titanic2, "sex", "class", "age", "AggregationFunction" -> Mean]

Here we replace the missing values with 0:

% /. Missing[] -> 0

Tips data

See the attached notebook how to massage the data and apply ToWideForm to
the “tips” table
in the question / book in order to obtain the posted in the question result.

The-main-function-I-miss-in-Mathematica-after-reading-a-Python-Pandas-Book.nb (1.04 MB)

5 Likes