Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Formatting output of series in python pandas

Here is my DataFrame. This is a representation of an 8-hour day, and the many different combinations of schedules. The time is in 24hr time.
Input:

solutions = problem.getSolutions()
pd.options.display.max_columns = None
df = pd.DataFrame(solutions)

Output:

   WorkHr1 WorkHr2 WorkHr3 WorkHr4  WorkOut  Lunch   FreeHour  Cleaning
0       13     14     15     16        11     10         9        12
1       13     14     15     16        11     10        12         9
2       13     14     15     16        11     12        10         9
3       13     14     15     16        11     12         9        10
4       13     14     15     16        12     11        10         9
..     ...    ...    ...    ...       ...    ...       ...       ...

I can create a series using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

series1 = pd.Series(solutions[0])
print(series1)

And I get this output:

WorkHr1     13
WorkHr2     14
WorkHr3     15
WorkHr4     16
WorkOut     11
Lunch       10
FreeHour     9
Cleaning    12

How can I switch the columns of this series so that the time is first?

Also, is there any possible way to display the rows in order of time? Like this:

  9    FreeHour
 10       Lunch
 11     WorkOut
 12    Cleaning
 13     WorkHr1
 14     WorkHr2
 15     WorkHr3
 16     WorkHr4

>Solution :

You can reverse it by passing its index as data and data as index to a Series constructor:

out = pd.Series(s.index, index=s).sort_index()

Output:

9     FreeHour
10       Lunch
11     WorkOut
12    Cleaning
13     WorkHr1
14     WorkHr2
15     WorkHr3
16     WorkHr4
dtype: object
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading