根据单独列中的值替换列的值

时间:2017-12-20 01:03:20

标签: python python-3.x pandas apply

我有一个看起来像的pandas DataFrame:

  ID  |   StateName   |   ZipCode
____________________________________  
  0          MD            20814     
  1                        90210   
  2          DC            20006   
  3                        05777   
  4                        12345

我有一个函数,它将根据ZipCode值填充StateName:

def FindZip(x):
    search = ZipcodeSearchEngine()
    zipcode = search.by_zipcode(x)
    return zipcode['State']

我想根据相应ZipCode的值填写StateName列中的空格。我没有成功尝试过这个:

test['StateName'] = test['StateName'].apply(lambda x: FindZip(test['Zip_To_Use']) if x == "" else x)

基本上,我想将一个函数应用于与我想要更改的列不同的列。我将不胜感激任何帮助!谢谢!

2 个答案:

答案 0 :(得分:2)

您可以尝试以下操作:

test['StateName'] = test.apply(lambda x: FindZip(test['Zip_To_Use']) 
                                      if x['StateName'] == "" 
                                      else x['StateName'], axis = 1)

上述代码适用于数据框而不是StateName,使用axis = 1适用于列。

更新

if语句中更新了多个条件(查看下面的解决方案):

test['StateName'] = test.apply(lambda x: FindZip(test['Zip_To_Use']) 
                                if ((x['StateName'] == "") and  (x['Zip_To_Use'] != ""))
                                else x['StateName'], axis = 1)

答案 1 :(得分:0)

我想出了一个不太“宽松”的解决方法。如果有人有想法,我仍然希望看到更“pythonic”或“pandorable”的解决方案!我基本上创建了一个与DataFrame长度相同的新列表,并遍历每一行,然后使用新列表在列上写下。

state = [FindState(test['Zip_To_Use'].iloc[i]) if (test['StateName'].iloc[i] == "" and test['Zip_To_Use'].iloc[i] != "") 
         else test['StateName'].iloc[i] for i in range(len(test))]

在常规for循环中重复(为了便于阅读):

state = []
for i in range(len(test)):
    if (test['StateName'].iloc[i] == "" and test['Zip_To_Use'].iloc[i] != ""):
        state.append(FindState(test['Zip_To-Use'].iloc[i]))
    else:
        state.append(test['StateName'].iloc[i])

然后用这个新列表重新分配列

test['StateName'] = state

如果您有更好的解决方案,请与我们联系!