相当于fct_reorder的熊猫

时间:2019-07-23 10:10:39

标签: python r pandas categorical-data

是否有一种方法可以根据熊猫数据框列与同一数据框中另一个分类列的关系来重新排序,类似于R中forcats包中的library(dplyr) x %>% mutate(month = factor(format(date, "%b"), levels = month.abb), year = format(date, "%Y")) %>% group_by(month, year) %>% summarise(total = sum(value)) %>% tidyr::spread(month, total, fill = 0) # A tibble: 2 x 8 # year Jan Feb Mar May Sep Nov Dec # <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #1 2018 0 0 0 0 4055. 2504. 869. #2 2019 563. 992. 1932. 3374. 0 0 0 ? 我的一个朋友想运行一个Python脚本,该脚本将从plotnine绘制出绘图。

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

x <- structure(list(customerId = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 
1L, 1L, 2L, 2L, 1L), .Label = c("A", "B"), class = "factor"), 
date = structure(c(17785, 17786, 17799, 17856, 17856, 17886, 
17901, 17947, 17960, 18017, 18044), class = "Date"), value = 
c(1180.00123428646, 
1516.3559531793, 1358.95010293461, 654.387023998424, 1849.73745560274, 
869.13160153199, 563.089300296269, 991.881078924052, 1931.75547372084, 
1834.30897409562, 1539.2051092349)), class = "data.frame", row.names = c(NA, -11L))

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

要将其读入R会话作为小标题:

fct_reorder

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

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

我的R代码是:

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"))

enter image description here

df = pd.read_clipboard()

enter image description here

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

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

platenine输出如下所示: enter image description here

问题是,如何告诉熊猫根据# 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 列对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') 列进行重新排序(即将颜色组合在一起)?

0 个答案:

没有答案