Home > Enterprise >  Formatting output of series in python pandas
Formatting output of series in python pandas

Time:02-23

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:

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

CodePudding user response:

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
  • Related