zip参数#1在循环zip时必须支持迭代错误

时间:2017-12-16 17:39:47

标签: python python-2.7 pandas

我看到很少有其他类似的问题报道,但是在我的情况下,我无法复制该解决方案。

我的问题更简单,因为我有一个数字列表和一个字符串列表

number = [21, 44, 31, 553, 63, 35]
access = ["denied", "Try Again", "Retry", "Accepted", "Error", "Success"]

我已将它们压缩并创建了一对包含这个

的每个值
testInput = zip(number, access)

output: 
[(21, 'denied'), (44, 'Try Again'), (31, 'Retry'), (553, 'Accepted'), (63, 'Error'), (35, 'Success')]

我正在尝试遍历每一对并执行我的函数,根据对的值将它们映射到团队名称。这是我的尝试:

def mapping(number, access):
    team = ''
    checkNumberAndAccess = zip(number, access)
    for number, access in checkNumberAndAccess:
        if number in range(20,30):
            team = 'Revolt'
        elif (number in range(40,50)) and (access == 'Try Again'):
            team = 'Strike'
        elif (number in range(60,100)) and (access == 'Error'):
            team = 'Exception'
    print team
    return team

我想要'团队'变量来保存每对的映射输出的值,所以这就是我执行函数的地方:

for number, access in testInput:
    Team = mapping(number, access)
    df = df.append({'Access Message': access, 'Number': number}, ignore_index=True)
print df

我得到" TypeError:zip参数#1必须支持迭代"执行映射功能时出错。它在错误的地方吗?

完整代码:

import pandas as pd

df = pd.DataFrame()


number = [21, 44, 31, 553, 63, 35]
access = ["denied", "Try Again", "Retry", "Accepted", "Error", "Success"]


def mapping(number, access):
    team = ''
    checkNumberAndAccess = zip(number, access)
    for number, access in checkNumberAndAccess:
        if number in range(20,30):
            team = 'Revolt'
        elif (number in range(40,50)) and (access == 'Try Again'):
            team = 'Strike'
        elif (number in range(60,100)) and (access == 'Error'):
            team = 'Exception'
    print team
    return team


testInput = zip(number, access)
print testInput

for number, access in testInput:
    Team = mapping(number, access)
    df = df.append({'Access Message': access, 'Number': number}, ignore_index=True)
print df

2 个答案:

答案 0 :(得分:1)

熊猫解决方案怎么样?

number = [21, 44, 31, 553, 63, 35]
access = ["denied", "Try Again", "Retry", "Accepted", "Error", "Success"]

#create DataFrame
df = pd.DataFrame({'number':number, 'access':access})

#create boolean masks
m1 =  df['number'].isin(range(20,30))
m2 =  df['number'].isin(range(40,50)) & (df['access'] == 'Try Again')
m3 =  df['number'].isin(range(60,100)) & (df['access'] == 'Error')

#create new column by conditions
df['Access Message'] = np.select([m1, m2,m3], ['Revolt','Strike','Exception'], default='')
print (df)
      access  number Access Message
0     denied      21         Revolt
1  Try Again      44         Strike
2      Retry      31               
3   Accepted     553               
4      Error      63      Exception
5    Success      35               

在您的解决方案中,可以在循环中将输出附加到list并最后通过构造函数创建DataFrame

number = [21, 44, 31, 553, 63, 35]
access = ["denied", "Try Again", "Retry", "Accepted", "Error", "Success"]

def mapping(number, access):
    out = []
    checkNumberAndAccess = zip(number, access)
    for number, access in checkNumberAndAccess:
        if number in range(20,30):
            out.append('Revolt')
        elif (number in range(40,50)) and (access == 'Try Again'):
            out.append('Strike')
        elif (number in range(60,100)) and (access == 'Error'):
            out.append('Exception')
        else:
            #add default value  
            out.append('')
    return out

access = mapping(number, access)

df = pd.DataFrame({'Access Message': access, 'Number': number})
print (df)
  Access Message  Number
0         Revolt      21
1         Strike      44
2                     31
3                    553
4      Exception      63
5                     35

答案 1 :(得分:0)

试试这个。

import pandas as pd

df = pd.DataFrame()


number = [21, 44, 31, 553, 63, 35]
access = ["denied", "Try Again", "Retry", "Accepted", "Error", "Success"]


def mapping(number, access):
    team = ''

    if number in range(20,30):
        team = 'Revolt'
    elif (number in range(40,50)) and (access == 'Try Again'):
        team = 'Strike'
    elif (number in range(60,100)) and (access == 'Error'):
        team = 'Exception'
    print team
    return team


testInput = zip(number, access)
print testInput

for number, access in testInput:
    Team = mapping(number, access)
    df = df.append({'Access Message': access, 'Number': number}, ignore_index=True)
print df

或者你可以从这里传递总列表并在那里压缩,处理并将最终结果返回给调用函数。希望这有帮助