gpt4 book ai didi

python - 用组 python 填充数据框的列

转载 作者:太空宇宙 更新时间:2023-11-04 00:34:56 26 4
gpt4 key购买 nike

我有一个 Pandas 数据框其中有 1 行

key
1
2
3
...
93

我没有。机器数量 = 3

我想分配相等的号码。每台机器的 key 。即

有9把 key 和3台机器,所以,每台机器应该关联3把 key 。以下是所需的数据框:

key    machine allocated    
1 1
2 1
3 1
..........

90 3
91 1
92 1
93 1

我们可以使用 groupby 来实现吗?

最佳答案

你可以使用arange的floor划分:

df = pd.DataFrame({'key' : range(1, 10)})

N = 3
N1 = len(df.index) / N

df['machine allocated'] = ((np.arange(len(df.index)) // N1) + 1).astype(int)
print (df)
key machine allocated
0 1 1
1 2 1
2 3 1
3 4 2
4 5 2
5 6 2
6 7 3
7 8 3
8 9 3

N = 2
N1 = len(df.index) / N

df['machine allocated'] = ((np.arange(len(df.index)) // N1) + 1).astype(int)
print (df)
key machine allocated
0 1 1
1 2 1
2 3 1
3 4 1
4 5 1
5 6 2
6 7 2
7 8 2
8 9 2

关于python - 用组 python 填充数据框的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44453986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com