定期和随机更新熊猫值?

问题描述 投票:0回答:1

我想使用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),只要它们可用即可。有没有办法直接在熊猫中这样做?

谢谢。 BBQuercus

python pandas dataframe
1个回答
0
投票

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

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是所需的结果。

© www.soinside.com 2019 - 2024. All rights reserved.