如何在python中将方法/函数应用于数据框

时间:2019-12-15 18:14:06

标签: python pandas dataframe

功能是:

def combine(row):
    count = 0
    stage = ""
    if (str(row.doggo) == "doggo"):
        stage = "doggo"
        count += 1

    if row.floofer == "floofer":
        stage = "floofer"
        count += 1

    if row.pupper == "pupper":
        stage = "pupper"
        count += 1

    if row.pupper == "puppo":
        stage = "puppo"
        count += 1

    if count == 0:
        return pd.np.NAN
    elif count == 1:
        return stage
    else:
        return "multiple"

在调用方法时:

df_twitter_archive_clean["status"] = df_twitter_archive_clean[["doggo","floofer","pupper","puppo"]].apply(combine)

出现错误信息:

  

-------------------------------------------------- ---------------------------- AttributeError Traceback(最近一次调用   最后)   ----> 1 df_twitter_archive_clean [“ status”] = df_twitter_archive_clean [[“ doggo”,“ floofer”,“ pupper”,“ puppo”]]。apply(combine)

     

〜/ anaconda3 / lib / python3.7 / site-packages / pandas / core / frame.py在   Apply(self,func,axis,broadcast,raw,reduce,result_type,args,   ** kwds)6485 args = args,6486 kwds = kwds)   -> 6487 return op.get_result()6488 6489 def applymap(self,func):

     

〜/ anaconda3 / lib / python3.7 / site-packages / pandas / core / apply.py在   get_result(个体)       149返回self.apply_raw()       150   -> 151返回self.apply_standard()       152       153 def apply_empty_result(self):

     

〜/ anaconda3 / lib / python3.7 / site-packages / pandas / core / apply.py在   apply_standard(个体)       255       256#使用序列生成器计算结果   -> 257 self.apply_series_generator()       258       259#换行结果

     

〜/ anaconda3 / lib / python3.7 / site-packages / pandas / core / apply.py在   apply_series_generator(个体)       284尝试:       对于枚举(series_gen)中的i,v,为285:   -> 286个结果[i] = self.f(v)       287个key.append(v.name)       288例外,例如e:

     

in Combine(行)         2计数= 0         3个阶段=“”   ----> 4 if(str(row.doggo)==“ doggo”):         5个阶段=“ doggo”         6个计数+ = 1

     

〜/ anaconda3 / lib / python3.7 / site-packages / pandas / core / generic.py在    getattr (自身,名称)5065(如果self._info_axis._can_hold_identifiers_and_holds_name(名称):5066
  返回自我[姓名]   -> 5067返回对象。 getattribute ((自身,名称))5068 5069 def setattr ((自身,名称,值):

     

AttributeError :(“系列”对象没有属性“ doggo”,“发生在索引doggo上”)

1 个答案:

答案 0 :(得分:1)

尝试:

df_twitter_archive_clean["status"] = df_twitter_archive_clean[["doggo","floofer","pupper","puppo"]].apply(combine, axis=1)

否则,您应按列应用该函数,如果我理解正确,则应按行应用