jonesdi
jonesdi

Reputation: 13

Python series repeat values

i have the following exercise as introduction to pandas:

Create a Series with the following content: index are days of the month April 2017 (from 1 to 30), and values are days of the week which are numbers from 1 to 7 (1st of April was Saturday).

i have tried to do it in the following way:

days = np.resize(np.arange(1,8), len(np.arange(1,31))) april = pd.Series(days, index = [np.arange(1,31)])

it allows me to repeat values 1 to 7 through the indexes, but i don't undesrtand how can i start it from 6 (Sat) instead of 1. or should i do it instead with a for loop to fill series with values?

Upvotes: 1

Views: 58

Answers (1)

Bruno Mello
Bruno Mello

Reputation: 4618

You can sum 5 and get module 7 as follows:

days = np.resize((np.arange(7)+5)%7+1, len(np.arange(1,31)))
april = pd.Series(days, index = [np.arange(1,31)])

Which returns:

1     6
2     7
3     1
4     2
5     3
6     4
7     5
8     6
9     7
10    1
11    2
12    3
13    4
14    5
15    6
16    7
17    1
18    2
19    3
20    4
21    5
22    6
23    7
24    1
25    2
26    3
27    4
28    5
29    6
30    7
dtype: int64

Upvotes: 1

Related Questions