Home > database >  create dataframe with increasing numbers in python
create dataframe with increasing numbers in python

Time:03-24

I want to create the following dataframe: n is the number of rows, and m is the columns. In R, this would be generated by:

ia=array((1:m),c(m,n))

But I do not know how i can achieve the same in python.

enter image description here

Kind regards,

CodePudding user response:

Use numpy.broadcast_to with DataFrame constructor:

m = 24
n = 13
df = pd.DataFrame(np.broadcast_to(np.arange(1, m   1)[:, None], (m, n)))
print (df)
    0   1   2   3   4   5   6   7   8   9   10  11  12
0    1   1   1   1   1   1   1   1   1   1   1   1   1
1    2   2   2   2   2   2   2   2   2   2   2   2   2
2    3   3   3   3   3   3   3   3   3   3   3   3   3
3    4   4   4   4   4   4   4   4   4   4   4   4   4
4    5   5   5   5   5   5   5   5   5   5   5   5   5
5    6   6   6   6   6   6   6   6   6   6   6   6   6
6    7   7   7   7   7   7   7   7   7   7   7   7   7
7    8   8   8   8   8   8   8   8   8   8   8   8   8
8    9   9   9   9   9   9   9   9   9   9   9   9   9
9   10  10  10  10  10  10  10  10  10  10  10  10  10
10  11  11  11  11  11  11  11  11  11  11  11  11  11
11  12  12  12  12  12  12  12  12  12  12  12  12  12
12  13  13  13  13  13  13  13  13  13  13  13  13  13
13  14  14  14  14  14  14  14  14  14  14  14  14  14
14  15  15  15  15  15  15  15  15  15  15  15  15  15
15  16  16  16  16  16  16  16  16  16  16  16  16  16
16  17  17  17  17  17  17  17  17  17  17  17  17  17
17  18  18  18  18  18  18  18  18  18  18  18  18  18
18  19  19  19  19  19  19  19  19  19  19  19  19  19
19  20  20  20  20  20  20  20  20  20  20  20  20  20
20  21  21  21  21  21  21  21  21  21  21  21  21  21
21  22  22  22  22  22  22  22  22  22  22  22  22  22
22  23  23  23  23  23  23  23  23  23  23  23  23  23
23  24  24  24  24  24  24  24  24  24  24  24  24  24

df = df.rename(index = lambda x: x 1, columns=lambda x: x 1)
print (df)
    1   2   3   4   5   6   7   8   9   10  11  12  13
1    1   1   1   1   1   1   1   1   1   1   1   1   1
2    2   2   2   2   2   2   2   2   2   2   2   2   2
3    3   3   3   3   3   3   3   3   3   3   3   3   3
4    4   4   4   4   4   4   4   4   4   4   4   4   4
5    5   5   5   5   5   5   5   5   5   5   5   5   5
6    6   6   6   6   6   6   6   6   6   6   6   6   6
7    7   7   7   7   7   7   7   7   7   7   7   7   7
8    8   8   8   8   8   8   8   8   8   8   8   8   8
9    9   9   9   9   9   9   9   9   9   9   9   9   9
10  10  10  10  10  10  10  10  10  10  10  10  10  10
11  11  11  11  11  11  11  11  11  11  11  11  11  11
12  12  12  12  12  12  12  12  12  12  12  12  12  12
13  13  13  13  13  13  13  13  13  13  13  13  13  13
14  14  14  14  14  14  14  14  14  14  14  14  14  14
15  15  15  15  15  15  15  15  15  15  15  15  15  15
16  16  16  16  16  16  16  16  16  16  16  16  16  16
17  17  17  17  17  17  17  17  17  17  17  17  17  17
18  18  18  18  18  18  18  18  18  18  18  18  18  18
19  19  19  19  19  19  19  19  19  19  19  19  19  19
20  20  20  20  20  20  20  20  20  20  20  20  20  20
21  21  21  21  21  21  21  21  21  21  21  21  21  21
22  22  22  22  22  22  22  22  22  22  22  22  22  22
23  23  23  23  23  23  23  23  23  23  23  23  23  23
24  24  24  24  24  24  24  24  24  24  24  24  24  24

CodePudding user response:

You can use np.repeat or np.tile

n = 5  # 13
m = 8  # 24

# Enhanced by @mozway
df = pd.DataFrame(np.tile(np.arange(1, m 1),(n, 1)).T)

# OR

df = pd.DataFrame(np.repeat(np.arange(1, m 1), m).reshape(-1, m))

print(df)

# Output
   0  1  2  3  4
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  5  5  5  5  5
5  6  6  6  6  6
6  7  7  7  7  7
7  8  8  8  8  8
  • Related