how to update each series field in a dataframe

Viewed 42

I have a DataFrame which holds two columns like below:

    player_id    days
0     None        1
1     None        1
2     None        1
3     None        1
4     None        1
5     None        1
6     None        2
7     None        2
8     None        2
9     None        2
10    None        2
.
.
82    None        13
83    None        14
83    None        14
83    None        14
83    None        14
83    None        14
83    None        14

in output, I need to replace None with the id of players which is 1 to 11, have something like:

    player_id    days
0     1           1
1     2           1
2     3           1
3     4           1
4     5           1
5     6           1
6     7           2
7     8           2
8     9           2
9     10          2
10    11          2
11    1           2
12    2           2
13    3           2
14    4           2
.
.
82    5           13
83    6           14
83    7           14
83    8           14
83    9           14
83    10          14
83    11          14

this is my code:

for index in range(len(df)):
    for i in range(1, 11):
       df.iloc[index, 0] = i

print(df)

however I get the following dataframe:

    player_id    days
0     11           1
1     11           1
2     11           1
3     11           1
4     11           1
5     11           1
6     11           2
7     11           2
8     11           2
9     11           2
10    11           2
11    11           2
12    11           2
13    11           2
14    11           2
.
.
82    11           13
83    11           14
83    11           14
83    11           14
83    11           14
83    11           14
83    11           14

I also tried to add a new series as follows, but does not work:

for index in range(len(df)):
    for i in range(1, 11):
       df.iloc[index, 0] = pd.Series([i, df['day']], index=['player_id', 'day'])

print(df)

I have some doubt if editing a filed in dataframe is possible or not, I just skipped itertuples and iterrows to be able to edit this rows in an efficient way.

2 Answers

try % operator:

import numpy as np
df['player_id'] = 1 + np.arange(len(df))%11
df

output


    player_id   days
0   1   1
1   2   1
2   3   1
3   4   1
4   5   1
5   6   1
6   7   2
7   8   2
8   9   2
9   10  2
10  11  2
82  1   13
83  2   14
83  3   14
83  4   14
83  5   14
83  6   14
83  7   14

Edit: using index

if the df's index (the first column in the output above) is not sequential and you want the same pattern but based on the index, then you can do

df['player_id'] = 1 + df.index%11

This can be done as.


i=0
for index in range(len(df)):
        df.iloc[index, 0] = 1+i%11
        i+=1

print(df)

   player_id  days
0          1     1
1          2     1
2          3     1
3          4     1
4          5     1
5          6     1
6          7     1
7          8     1
8          9     1
9         10     1
10        11     1
11         1     2
12         2     2
13         3     2
14         4     2
15         5     2
16         6     2
17         7     2
18         8     2
19         9     2
20        10     2
21        11     2
22         1     3
23         2     3
24         3     3
25         4     3
26         5     3
27         6     3
28         7     3
29         8     3
30         9     3
31        10     3
32        11     3
Related