2

I'm using Python module like Pandas, Matplotlib to make charts for a university Project. I got some problems ordering the result in the pivot Table.

This is the body of a function, that takes 3 lists in input ([2017-03-03, ...], ['Username1', 'Username2',...], [1012020,103024,...]), analyze data and makes chart about it.

data = [date_list,username,field]
username_no_dup = list(set(username))

rows = zip(data[0], data[1], data[2])
headers = ['Date', 'Username', 'Value']
df = pd.DataFrame(rows, columns=headers)
df = df.sort_values('Value', ascending=False)
#*sort_values works but it is not sorting when converting to Pivot Table*
pivot_df = pd.pivot_table(df ,index='Date', columns='Username', values='Value')
pivot_df.loc[:,username_no_dup].plot(kind='bar', stacked=True, color=color_list, figsize=(15,7))

I would like to order by values with the greater value near the X-line of the chart. Everyone solved this problem??? Thank you

Here is the top rows of df sorted by value:

[['2017-03-15','SSL1_APP',1515091]
['2017-03-16','SSL1_APP',1373827]
['2017-03-18','SSL1_APP',1136483]
['2017-03-21','SSL1_APP',601810]
['2017-03-17','SSL1_APP',325561]
['2017-03-15','KE77_APP',284971]
['2017-03-16','AF77_APP',222588]
['2017-03-16','MI77_APP',222148]
['2017-03-15','AF77_APP',202224]
['2017-03-15','MI77_APP',191791]
['2017-03-17','AF77_APP',187709]
['2017-03-16','PC77_APP',185766]
['2017-03-15','NE77_APP',177475]
['2017-03-18','FBW2_APP',175156]
['2017-03-16','NE77_APP',174570]
['2017-03-17','BFD1_APP',164238]
['2017-03-15','BFD1_APP',162931]
['2017-03-20','AF77_APP',152186]
['2017-03-17','PC77_APP',148727]
['2017-03-18','MI77_APP',147460]
['2017-03-16','BFD1_APP',145815]
['2017-03-20','BFD1_APP',145449]
['2017-03-15','PC77_APP',144959]
['2017-03-20','SSL1_APP',141719]]

The first pic is the plot I have created. The second one is the result I want, plotted with Excel:

enter image description here

James
  • 32,991
  • 4
  • 47
  • 70
Marco
  • 21
  • 3

1 Answers1

0

Note: This is a Python answer on the subject sorting your input.

One way of doing this would be using a bidimensional list(A lists of lists) and then sorting it.

This is how you've been using it:

data = [date0,username0,randint0,date1,username1, ....

Try a bidimensional list instead:

data = [[date0,username0,randint0], [date1,username1,randint1]...

Use the .sort() method and change the syntax to look like this:

data.sort() #Sort it, by default it will be a decreasing list.
rows = zip(data[0][0], data[0][1], data[0][2])

The standard .sort() method has its limitations(floats for one) so if doesn't return a desirable output try using .sort() parameters, here is a insight on the subject: How to use .sort()

If you are having trouble with floats, check a answer that will help you here.

Community
  • 1
  • 1
Mitrek
  • 1,222
  • 8
  • 10
  • I've tried that solution but with no good result! Isn't possible to sort the pivot table? Because this gaves to me the wanted result....but not sorted... – Marco Mar 21 '17 at 15:44