简体   繁体   English

ValueError:只能将大小为 1 的数组转换为 Python 标量

[英]ValueError: can only convert an array of size 1 to a Python scalar

I have a problem very similar to this one here:我有一个与这里非常相似的问题:

Count all subordinates who directly or indirectly reports to managers 统计所有直接或间接向经理汇报的下属

I do have a table with all employees in one column and their resp.我确实有一个表格,其中包含一列中的所有员工及其代表。 boss_id in the other. boss_id 在其他。

    employee_id boss_id receive_reports nr_reports
0   46456       175361  False           0
1   104708      29733   False           0
2   120853      41991   False           0
3   142630      171266  True            1
4   72711       198240  False           0

As a result I want a table, where for every employee I read the number of all direct and indirect reports (eg 0 for all people on the first level, up to #allemployees for the CEO).因此,我想要一个表格,我在其中读取每个员工的所有直接和间接报告的数量(例如,第一层的所有人员均为 0,CEO 最高为 #allemployees)。 In the last columns I easily could differentiate between first level people and everybody else.在最后几列中,我可以轻松区分第一级人员和其他所有人。

I want to use python and I've been wrapping my head around it for quite a while.我想使用 python 并且我一直在用它缠着我的头很长一段时间。 I do have a general idea: starting with all the people at the first level and zero reports, going up by one level etc...我确实有一个大致的想法:从第一层的所有人开始,零报告,再上一层等等......


EDIT 1编辑 1

here is my approach (I would be also curious to know if there is a better one)这是我的方法(我也很想知道是否有更好的方法)

I have a list with all level_0 employees.我有一个包含所有 level_0 员工的列表。

def sum_reports(current_level):
   # pass current level of employees as list
   if len(current_level) == 1:
       return
   else:
       next_level = []
       #find boss for every employees at current level and add to next level + add to report of this boss 1
       for emp in current_level:
           tmp_boss = data.loc[data.employee_id == emp, "boss_id"].item()
           next_level.append(tmp_boss)
           data.loc[(data.employee_id == tmp_boss),"nr_reports"] += 1
       sum_reports(next_level)

   sum_reports(level_0)

But I do get the error:但我确实收到错误:

ValueError: Can only compare identically-labeled Series objects

EDIT 2编辑 2

Got rid of the comparison error in adding .item() changing the line of code with tmp_boss = ...摆脱了添加.item()更改代码with tmp_boss = ...的比较错误with tmp_boss = ...

But now I do get another error, which I cannot track: ValueError: can only convert an array of size 1 to a Python scalar但现在我确实收到另一个错误,我无法跟踪: ValueError: can only convert an array of size 1 to a Python scalar

I could fix the problem, doing the following:我可以解决这个问题,执行以下操作:

  • add a check, if a boss is already in my next level (as there are several reports to the same person)添加一个检查,如果老板已经在我的下一个级别(因为有多个报告给同一个人)

  • and check if the the boss_id is '0' - an entry I replaced the NaN for the CEO with.并检查 boss_id 是否为“0”——我用一个条目替换了 CEO 的 NaN。 This actually caused the conversion error这实际上导致了转换错误

    • to get along with imbalance in the tree (people directly reporting on higher level) I had to add another check - if there is a more concise or elegant solution, let me know.为了处理树中的不平衡(人们直接向更高级别报告)我不得不添加另一个检查 - 如果有更简洁或更优雅的解决方案,请告诉我。

Here is the final code:这是最终的代码:

#a list of the bosses column as the direct_reports
direct_reports = data.boss_id.tolist()
seen = []
def sum_reports(current_level):
    # pass current level of employees as list
    if len(current_level) <= 1:
        return
    else:
        next_level = []
        #find boss for every employee at current level and add to next level + add 1 to report of this boss
        for emp in current_level:
            tmp_boss = data.loc[data.employee_id == emp, "boss_id"].item()

            seen.append(tmp_boss)

            #only if is the last direct report: add boss to the next_level
            if direct_reports.count(tmp_boss) == seen.count(tmp_boss) and tmp_boss != 0:    
                next_level.append(tmp_boss)


            # add reports, plus 1 and the reports sofar of the current emp    
            sofar = data.loc[data.employee_id == emp, "nr_reports"].item()                
            data.loc[(data.employee_id == tmp_boss),"nr_reports"] += (sofar+1)    

        sum_reports(next_level)
# level_o is a list of all employees, who don't appear in the boss column
sum_reports(level_0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:只能将大小为 1 的数组转换为 Numpy Select 的 Python 标量 - ValueError: can only convert an array of size 1 to a Python scalar for Numpy Select Pandas ValueError:只能将大小为 1 的数组转换为 Python 标量 - Pandas ValueError: can only convert an array of size 1 to a Python scalar Python:在 pd.DataFrame 中循环行时,“ValueError:只能将大小为 1 的数组转换为 Python 标量” - Python: 'ValueError: can only convert an array of size 1 to a Python scalar' when looping over rows in pd.DataFrame 错误:只能将大小为 1 的数组转换为 Python 标量 - Error: can only convert an array of size 1 to a Python scalar Pandas - 只能将大小为 1 的数组转换为 Python 标量 - Pandas - can only convert an array of size 1 to a Python scalar Pandas 只能将大小为 1 的数组转换为 Python 标量 - Pandas can only convert an array of size 1 to a Python scalar 将大小为 1 的 Python 标量和 numpy 数组转换为标量 - convert Python scalar and numpy array of size 1 to scalar error “can only convert an array of size 1 to a Python scalar” when I try to get the index of a row of a pandas dataframe as an integer - error “can only convert an array of size 1 to a Python scalar” when I try to get the index of a row of a pandas dataframe as an integer 将数组转换为python标量 - Convert array to python scalar TypeError: 只有大小 -1 arrays 可以转换为 python 标量 - TypeError: only size -1 arrays can be converted to python scalar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM