简体   繁体   中英

In Python, why does pandas.Series.between function return error "'str' object has no attribute 'between'" in user-created function?

I am writing a function to classify ICD-10 codes into dummy variables for particular causes of death. The pandas.Series.between function works fine in a one-liner, but fails when placed in a user-created function.

When I create a dummy variable outside of a function, it works fine. For example:

df["copd"] = df["icd10"].between("j40", "j4799").astype(int)
df["copd"].value_counts()

0    41071
1     1957
Name: copd, dtype: int64

However, it throws an attribution error when I try to place this in a user-created function:


def classify_death(row):
     copd = row["icd10"].between("c00", 
     "c9799").astype(int)
     return copd

df["copd"] = df.apply(classify_death, axis=1)

...

\~\\AppData\\Local\\Temp\\ipykernel_4684\\1881079059.py in classify_death(row)
1 def classify_death(row):
\----\> 2     copd = row["dmcaacme"].between("c00", "c9799").astype(int)
3     return copd
4
5

AttributeError: 'str' object has no attribute 'between'

Any ideas? Many thanks in advance for any help!

No function is needed. Just apply.between directly to the column

df["copd"] = df['icd10'].between("j40", "j4799").astype(int)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM