1

got a quick question:

Equivalent code in R code:

mutate(states = reorder(states, median(age)))

I have this in Python:

states = ['California', 'New York', 'Texas', 'Florida']
test_df = pd.DataFrame({'states': states, 
                        'median':[36, 25, 30, 28]})
test_df['states'] = test_df['states'].astype('category')

How do i 'reorder' the 'states' column by median column, so the plot X axis will appear in the order of median column.

For example in R, the below is already in order of median_age.

> df <- df %>% mutate(states = reorder(states, median_age))
> df
      states median_age
1 California         36
2   New York         25
3      Texas         30
4    Florida         28

> df %>% ggplot(aes(states, median_age)) + geom_bar(stat="identity")

Axis X sequence follow median

ZenF
  • 55
  • 1
  • 5
  • IIUc use `df = test_df.sort_values('median')` – jezrael Aug 18 '20 at 07:20
  • sorry. i think i duplicated my question.. i have editted my question to show `df` is not sorted but the plot will appear sorted – ZenF Aug 18 '20 at 07:36
  • I think not possible in pandas/python, you need new dataframe `df` with sorted values for this output . – jezrael Aug 18 '20 at 07:39

0 Answers0