Python在Dataframe列中更改值

时间:2013-08-04 12:09:30

标签: python

我正在尝试通过使用遍历列的while循环将数据框列值更改为我生成的某个数字 数字(称为“num”)计算好。 我认为问题在于while循环结束时的行:

data.ix[i]['numOfDocuments'] = num

即使在尝试

时也是如此
data.ix[i]['numOfDocuments'] = 2

值保持为0(我使用打印数据[numOfDocuments] .sum())

以下是代码:

data['numOfDocuments'] = 0
i = 0
while (i < len(data)):
    old_str = data.ix[i]['Params']
    if (old_str != '{}') and (old_str.find('numOfDocuments') != -1):
        str = old_str.replace('{', '').replace('}', '')
        if (str.find(';',str.find('numOfDocuments')) == -1):
            num = int(str[(str.find('numOfDocuments') + 17)::])
        else:
            num = int(str[(str.find('numOfDocuments') + 17):(str.find(';',str.find('numOfDocuments')))])
    else:
        num = None
    data.ix[i]['numOfDocuments'] = num
    i += 1

0 个答案:

没有答案