Replace lines in pandas

Viewed 76

I am working with python to create a new frame starting from two frame by using Pandas. The first frame (called frame1) is composed by the following line:

A   B   C   D   E
1   1   1   1   1
2   2   2   2   2
3   3   3   3   3
4   4   4   4   4
5   5   5   5   5
6   6   6   6   6
7   7   7   7   7
8   8   8   8   8
9   9   9   9   9
10  10  10  10  10
11  11  11  11  11
12  12  12  12  12
13  13  13  13  13
14  14  14  14  14
15  15  15  15  15

The second frame (called frame2) is:

A   B   C   D   E

19  19  19  19  19
24  24  24  24  24
29  29  29  29  29
34  34  34  34  34
39  39  39  39  39
44  44  44  44  44
49  49  49  49  49
54  54  54  54  54
59  59  59  59  59
64  64  64  64  64
69  69  69  69  69
74  74  74  74  74
79  79  79  79  79
84  84  84  84  84
89  89  89  89  89
94  94  94  94  94
99  99  99  99  99

Now i want to create a new dataset with this logic: starting from frame1 substitute every 5 row until the end of the frame1, the row of the frame1 with a random row of the frame2 (and remove the added row from frame2). A possible output should be:

A   B   C   D   E
1   1   1   1   1
2   2   2   2   2
3   3   3   3   3
4   4   4   4   4
59  59  59  59  59
6   6   6   6   6
7   7   7   7   7
8   8   8   8   8
9   9   9   9   9
29  29  29  29  29
11  11  11  11  11
12  12  12  12  12
13  13  13  13  13
14  14  14  14  14
84  84  84  84  84

How can i do this operation?

2 Answers

It's quite simple:

frame1.loc[4::5] = frame2.sample(frac=1).reset_index(drop=True)

where

  • df.loc[4::5] selects every fifth element, starting with the fifth one in df, and
  • df.sample(frac=1).reset_index(drop=True) shuffles a df around randomly

One way is to first obtain the indices where to update (we could also slice assign, but we'd have the problem of the end not being included), and then assign back taking a sample from df2 of the corresponding size:

ix = np.flatnonzero(np.diff(np.arange(df.shape[0]+1)//5))
df1.iloc[ix] = df2.sample(df1.shape[0]//5).to_numpy()

print(df1)
     A   B   C   D   E
0    1   1   1   1   1
1    2   2   2   2   2
2    3   3   3   3   3
3    4   4   4   4   4
4   84  84  84  84  84
5    6   6   6   6   6
6    7   7   7   7   7
7    8   8   8   8   8
8    9   9   9   9   9
9   89  89  89  89  89
10  11  11  11  11  11
11  12  12  12  12  12
12  13  13  13  13  13
13  14  14  14  14  14
14  99  99  99  99  99
Related