Python 根据现有列中的值为dataframe列指定随机值
我正在尝试使用employee列中的值填充dataframe的Assignment列。我已将示例赋值粘贴到下表中。 分配值不能等于该行的管理器或下级列中的值。赋值值也不应重复。举个例子,因为Sue和Mary已经被分配了任务,所以他们不应该再被分配任务 我在上找到了和这篇文章的这些说明,但我仍停留在如何使用python执行中间步骤上Python 根据现有列中的值为dataframe列指定随机值,python,pandas,Python,Pandas,我正在尝试使用employee列中的值填充dataframe的Assignment列。我已将示例赋值粘贴到下表中。 分配值不能等于该行的管理器或下级列中的值。赋值值也不应重复。举个例子,因为Sue和Mary已经被分配了任务,所以他们不应该再被分配任务 我在上找到了和这篇文章的这些说明,但我仍停留在如何使用python执行中间步骤上 +----------+---------+-------------+-------------------+---------------------------
+----------+---------+-------------+-------------------+----------------------------------------+
| Employee | Manager | Subordinate | Manager Exclusion | Subordinate Exclusion | Assignment |
+----------+---------+-------------+-------------------+----------------------------------------+
| Jim | Joe | | 2 | | Mary |
| Joe | | Jim | | 1 | Sue |
| Sue | | David | | 5 | |
| Kelly | David | | 5 | | |
| David | Sue | Kelly | 3 | 4 | |
| Mary | Jim | | 1 | | |
+----------+---------+-------------+-------------------+----------------------------------------+
这里有一种使用循环和“随机”函数的方法。似乎有更有效的方法,但这对于一个简短的列表来说已经足够快了:
import pandas as pd
import random
import numpy as np
#Create the data above
data = {
'Employee':['Jim','Joe','Sue','Kelly','David','Mary'],
'Manager':['Joe','','','David','Sue','Jim'],
'Subordinate':['','Jim','David','','Kelly',''] }
df = pd.DataFrame(data)
#Create list to store 'used' assignments
used = []
#Make blank column for the Assignments
df['Assigned'] = ''
#Loop through the dataframe
for index,row in df.iterrows():
#Iterate through random choices until it chooses one that meets the criteria
while df.iat[index,3] == '':
rname = random.choice(df['Employee']) #Get random name from column 1
if (rname != row['Manager']) and (rname != row['Subordinate']) and (rname != row['Employee']) and (rname not in used):
df.iat[index,3] = rname #If it met the criteria, assign it.
used.append(rname) #Add to 'used' list so it won't be used again
这里有一种使用循环和“随机”函数的方法。似乎有更有效的方法,但这对于一个简短的列表来说已经足够快了:
import pandas as pd
import random
import numpy as np
#Create the data above
data = {
'Employee':['Jim','Joe','Sue','Kelly','David','Mary'],
'Manager':['Joe','','','David','Sue','Jim'],
'Subordinate':['','Jim','David','','Kelly',''] }
df = pd.DataFrame(data)
#Create list to store 'used' assignments
used = []
#Make blank column for the Assignments
df['Assigned'] = ''
#Loop through the dataframe
for index,row in df.iterrows():
#Iterate through random choices until it chooses one that meets the criteria
while df.iat[index,3] == '':
rname = random.choice(df['Employee']) #Get random name from column 1
if (rname != row['Manager']) and (rname != row['Subordinate']) and (rname != row['Employee']) and (rname not in used):
df.iat[index,3] = rname #If it met the criteria, assign it.
used.append(rname) #Add to 'used' list so it won't be used again
嗨,梅根,欢迎来到StackOverflow!在提问时,通常最好包含一些您尝试过的代码。通读是了解什么类型的问题在这里会很受欢迎的好方法。嗨,梅根,欢迎来到StackOverflow!在提问时,通常最好包含一些您尝试过的代码。通读是了解哪些类型的问题在这里会受到欢迎的好方法。