1

The function is:

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"

While calling the method:

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

The error message appears:

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in ----> 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 in 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 in get_result(self) 149 return self.apply_raw() 150 --> 151 return self.apply_standard() 152 153 def apply_empty_result(self):

~/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in apply_standard(self) 255 256 # compute the result using the series generator --> 257 self.apply_series_generator() 258 259 # wrap results

~/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in apply_series_generator(self) 284 try: 285 for i, v in enumerate(series_gen): --> 286 results[i] = self.f(v) 287 keys.append(v.name) 288 except Exception as e:

in combine(row) 2 count = 0 3 stage = "" ----> 4 if (str(row.doggo) == "doggo"): 5 stage = "doggo" 6 count += 1

~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in getattr(self, name) 5065 if self._info_axis._can_hold_identifiers_and_holds_name(name): 5066
return self[name] -> 5067 return object.getattribute(self, name) 5068 5069 def setattr(self, name, value):

AttributeError: ("'Series' object has no attribute 'doggo'", 'occurred at index doggo')

1
  • use axis=1 in apply() to indicate columns axis Commented Dec 15, 2019 at 18:36

1 Answer 1

1

Try:

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

Otherwise you apply the function column-wise and if I understand you correctly- you want it row-wise

Sign up to request clarification or add additional context in comments.

1 Comment

No worries, please consider upvoting, and mark this post as an answer to close off your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.