kikee1222
kikee1222

Reputation: 2026

Force a row for each day in Pandas

I have a dataset where the user inputs data for the days they work

Monday Tuesday Wednesday Etc..

I want the report to show a row per day, even if they didn't work on that day. So, if they haven't worked on a Monday, the values should be Null, but there should still be a row for Monday.

Is that possible?

Thanks

Upvotes: 0

Views: 15

Answers (1)

Corralien
Corralien

Reputation: 120509

Suppose the user input is a list of work days:

# Create an empty Series indexed by week days
idx = pd.Index(['Monday', 'Tuesday', 'Wednesday', 'Thursday',
                'Friday', 'Saturday', 'Sunday'])
sr = pd.Series(np.nan, index=idx)

# User input, here a simple list
l = ['Monday', 'Wednesday', 'Friday', 'Saturday']

# Fill the Series with the user input
sr[l] = 1

Output:

>>> sr
Monday       1.0
Tuesday      NaN
Wednesday    1.0
Thursday     NaN
Friday       1.0
Saturday     1.0
Sunday       NaN
dtype: float64

Upvotes: 1

Related Questions