Home > other >  subdividing the rows of data
subdividing the rows of data

Time:01-08

I have two columns data as

5   8.2
15  7.4
18  3.2
25  9.1

I want to subdivide each rows values with 2 so that data will be

2.5  4.1
2.5  4.1
7.5  3.7
7.5  3.7
9.0  1.6
9.0  1.6
12.5 4.55
12.5 4.55

Then i want to subdivide each row values by 10 and i want to write the same to a file. I tried the below code but it doesnot do the same thing what i expect.Hope experts may help me. Thanks.

import numpy as np
data=np.loadtxt('two_column.txt') 
data=data/2
data=data/10
np.savetxt('output.txt',data)

CodePudding user response:

With a DataFrame,you can replicate the rows with repeat and divide by N:

N = 2

out = df.loc[df.index.repeat(N)].div(N) # or .div(10*N) if wanted

Output:

      0     1
0   2.5  4.10
0   2.5  4.10
1   7.5  3.70
1   7.5  3.70
2   9.0  1.60
2   9.0  1.60
3  12.5  4.55
3  12.5  4.55

With :

out = np.repeat(df.to_numpy(), N, axis=0)/N

# or with an array A as input
out = np.repeat(A, N, axis=0)/N

Output:

array([[ 2.5 ,  4.1 ],
       [ 2.5 ,  4.1 ],
       [ 7.5 ,  3.7 ],
       [ 7.5 ,  3.7 ],
       [ 9.  ,  1.6 ],
       [ 9.  ,  1.6 ],
       [12.5 ,  4.55],
       [12.5 ,  4.55]])
  • Related