Skip to content
Advertisement

How can a create a percentage matrix based on a dataframe

I have a dataframe that looks like that :

JavaScript
Place A Place B Type Number
New York Paris A 34
Oslo London B 42
Oslo London A 24

i need to have the percentage number of each type according to the routes.

I don’t know witch command to use to get a dataframe that looks like this

xxx Paris Oslo London New York
Paris NaN NaN NaN type A : 100%
Oslo NaN NaN type A : 36,3% type B : 63,6% NaN
London NaN type A : 36,3% type B : 63,6% NaN NaN
New York type A : 100% NaN NaN NaN

Advertisement

Answer

IUUC, you could reshape the data, then compute the percent per group, finally reshape again:

JavaScript

output:

JavaScript

If you don’t need the symmetrical output, you can keep it by PlaceA/PlaceB, which will have the same information in condensed form:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement