假设我的df索引为16,我想平均分配数字A-L
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
我想要的输出是:
1 A
2 A
3 B
4 B
5 C
6 C
7 D
8 D
9 E
10 F
11 G
12 H
13 I
14 J
15 K
16 L
有没有办法做到这一点
rname是第二列 ,df2是第一列, 我的方式是np.repeat(rname,np.ceil(len(df2)/ len(rname)))[:len(df2)]
答案 0 :(得分:3)
您可以这样做:
df['new_col'] = np.sort((np.arange(16) % 12) +1)
答案 1 :(得分:1)
df = pd.read_clipboard(header=None)
ar_int = [int(x) for x in np.arange(1, 13, (12/16))]
df[1] = ar_int
print(df)
0 1
0 1 1
1 2 1
2 3 2
3 4 3
4 5 4
5 6 4
6 7 5
7 8 6
8 9 7
9 10 7
10 11 8
11 12 9
12 13 10
13 14 10
14 15 11
15 16 12
对于字符串注释,可以执行此操作。
alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ar_int = [alphabet[int(x)] for x in np.arange(0, 12, (12/16))]
df[1] = ar_int
print(df)
0 1
0 1 A
1 2 A
2 3 B
3 4 C
4 5 D
5 6 D
6 7 E
7 8 F
8 9 G
9 10 G
10 11 H
11 12 I
12 13 J
13 14 J
14 15 K
15 16 L
答案 2 :(得分:1)
这可能对您有帮助
import numpy as np
rname = ['A','B','C','D','E','F','G','H','I','J','K','L']
n_times = int(np.ceil(len(data) / len(rname)))
lst = np.repeat(rname,n_times)
n_elems = (len(df) - len(rname))
output = list(lst[:n_elems*n_times]) + list(rname[n_elems:])
print(output)
输出
['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']