首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >相当于fct_reorder的pandas

相当于fct_reorder的pandas
EN

Stack Overflow用户
提问于 2019-07-23 18:10:39
回答 1查看 240关注 0票数 0

有没有一种方法可以根据pandas数据框列与同一数据框中另一个分类列的关系对其进行重新排序,类似于R中forcats包中的fct_reorder?我的一个朋友想运行一个python脚本,它可以从plotnine中绘制出图。

reprex数据帧可以在下面找到:

代码语言:javascript
复制
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

我还制作了一个csv供下载:https://github.com/Biomiha/factors/blob/master/Fct_reorder_reprex.csv

要将其作为tibble读取到R会话中:

代码语言:javascript
复制
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"))

要将其作为Pandas DataFrame读取到python会话中,请复制上表并使用以下命令粘贴:

代码语言:javascript
复制
df = pd.read_clipboard()

我有的R代码是:

代码语言:javascript
复制
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

代码语言:javascript
复制
# 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

到目前为止,等效的python代码是:

代码语言:javascript
复制
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')

图九的输出如下所示:

问题是,我如何告诉pandas根据Group列对Name列进行重新排序(即将颜色组合在一起)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-06 22:02:50

两年过去了,但现在我们有了一个完美的python解决方案:

代码语言:javascript
复制
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
代码语言:javascript
复制
     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
代码语言:javascript
复制
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

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57161713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档