随机更新熊猫值?



我想用pandas在python中构建一个调度应用程序。

初始化以下数据帧,其中0表示人员是否忙碌,1人员是否有空。

import pandas as pd
df = pd.DataFrame({'01.01.': [1,1,0], '02.01.': [0,1,1], '03.01.': [1,0,1]}, index=['Person A', 'Person B', 'Person C']) 
>>> df
01.01.  02.01.  03.01.
Person A       1       0       1
Person B       1       1       0
Person C       0       1       1

我现在想随机安排每天n人数(如果有的话(。换句话说,对于每天,如果有人有空(1(,则随机设置n计划的人数(2(。

我尝试了如下方法:

# Required number of people across time / columns
required_number = [0, 1, 2]
# Iterate through time / columns
for col in range(len(df.columns)):
# Current number of scheduled people
current_number = (df.iloc[:, [col]].values==2).sum()
# Iterate through indices / rows / people
for ind in range(len(df.index)):
# Check if they are available (1) and
# if the required number of people has not been met yet
if (df.iloc[ind, col]==1 and
current_number<required_number[col]):
# Change "free" / 1 person to "scheduled" / 2
df.iloc[ind, col] = 2
# Increment scheduled people by one
current_number += 1
>>> df
01.01.  02.01.  03.01.
Person A       1       0       2
Person B       1       2       0
Person C       0       1       2

这按预期工作,但是 - 因为我只是循环,所以我无法添加随机性(即Person A / B / C(是随机选择的,只要它们可用。有没有办法直接在熊猫身上这样做?

谢谢。烧烤

您可以在序列中随机选择适当的索引,然后更改与所选索引对应的值:

for i in range(len(df.columns)):

if sum(df.iloc[:,i] == 1) >= required_number[i]:

column = df.iloc[:,i].reset_index(drop=True)
#We are going to store indices in a list 
a = [j for j in column.index if column[j] == 1]

random_indexes = np.random.choice(a, required_number[i], replace = False)

df.iloc[:,i] = [column[j] if j not in random_indexes else 2 for j in column.index]

现在df是想要的结果。

最新更新