Is there a way of reordering a pandas data frame column based on its relationship to another categorical column in the same data frame, akin to fct_reorder
from the forcats package in R?
A friend of mine wants to run a python script that would plot out the plots from plotnine.
The reprex data frame can be found below:
Group Name Height
0 3 Abigail 151.09962170955896
1 2 Amelia 144.53368144215813
2 1 Ava 150.84441176683055
3 2 Charlotte 144.2526003986535
4 3 Emily 150.01613555140298
5 1 Emma 127.9293425061458
6 3 Evelyn 154.35548000906718
7 3 Harper 155.22807300246453
8 1 Isabella 116.54302297370651
9 2 Mia 155.0605589215757
10 1 Olivia 142.7742924211066
11 2 Sophia 154.2912468881105
I've also made a csv for download: https://github.com/Biomiha/factors/blob/master/Fct_reorder_reprex.csv
To read it into an R session as a tibble:
df <- structure(list(Group = c(3, 2, 1, 2, 3, 1, 3, 3, 1, 2, 1, 2),
Name = c("Abigail", "Amelia", "Ava", "Charlotte", "Emily",
"Emma", "Evelyn", "Harper", "Isabella", "Mia", "Olivia",
"Sophia"), Height = c(151.099621709559, 144.533681442158,
150.844411766831, 144.252600398653, 150.016135551403, 127.929342506146,
154.355480009067, 155.228073002465, 116.543022973707, 155.060558921576,
142.774292421107, 154.29124688811)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), spec = structure(list(
cols = list(Group = structure(list(), class = c("collector_double",
"collector")), Name = structure(list(), class = c("collector_character",
"collector")), Height = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
And to read it into a python session as a Pandas DataFrame, copy the table above and paste using:
df = pd.read_clipboard()
The R code I have is:
library(tidyverse)
# The unordered plot that is the default looks like:
plot_without <- df %>%
dplyr::mutate(Group = as.factor(Group)) %>%
ggplot(aes(x = Name, y = Height, fill = Group)) +
geom_bar(stat = "identity") +
labs(title = "Plot without ordering")
plot_without
# To order the 'Name' variable, using fct_reorder (this is what I want but from python):
plot_with <- df %>%
dplyr::mutate(Group = as.factor(Group),
Name = fct_reorder(Name, Group, identity)) %>%
ggplot(aes(x = Name, y = Height, fill = Group)) +
geom_bar(stat = "identity") +
labs(title = "Ordered plot")
plot_with
The equivalent python code so far is:
import sys
import pandas as pd
from plotnine import *
df=pd.read_csv('Fct_reorder_reprex.csv')
df['Group'] = df['Group'].astype('category')
ggplot(df) + geom_bar(aes(x = 'Name', y = 'Height', fill = 'Group', col = 'Group'), stat = 'identity') + labs(title='Python unordered plot')
The plotnine output looks like the following:
The question is, how do I tell pandas to reorder the Name
column based on the Group
column (i.e. to make the colours group together)?
It's been two years, but now we have a perfect solution in python:
import pandas as pd
from datar.all import f, mutate, fct_reorder, as_factor, identity
from plotnine import ggplot, geom_bar, labs, aes
df = pd.read_csv("https://github.com/Biomiha/factors/raw/master/Fct_reorder_reprex.csv")
df
Group Name Height
<int64> <object> <float64>
0 3 Abigail 151.099622
1 2 Amelia 144.533681
2 1 Ava 150.844412
3 2 Charlotte 144.252600
4 3 Emily 150.016136
5 1 Emma 127.929343
6 3 Evelyn 154.355480
7 3 Harper 155.228073
8 1 Isabella 116.543023
9 2 Mia 155.060559
10 1 Olivia 142.774292
11 2 Sophia 154.291247
plot_without = (
df
>> mutate(Group=as_factor(f.Group))
>> ggplot(aes(x="Name", y="Height", fill="Group"))
+ geom_bar(stat="identity")
+ labs(title="Plot without ordering")
)
plot_without
plot_with = (
df
>> mutate(Group=as_factor(f.Group), Name=fct_reorder(f.Name, f.Group, _fun=identity))
>> ggplot(aes(x="Name", y="Height", fill="Group"))
+ geom_bar(stat="identity")
+ labs(title="Plot without ordering")
)
plot_with