'numpy.int64' object has no attribute 'startswith'解决办法

想要通过遍历数据中的给出的几个列,匹配某些字符开头的字符串,如果有,则新建的last列的数值加2

Python原始代码如下:

import pandas as pd
import numpy as np
patient=pd.read_csv(r'C:\Users\Administrator\full_cohort_data2.csv')
last = np.zeros(patient.shape[0])
alist=['Fri','Sa','Th']
colum=['day_icu_intime','service_num','service_unit']
def charlson(day):
    for i in range(1776):
        for j in range(len(alist)):           
            if patient[day][i].startswith(alist[j]):
                last[i]+=2
for k in range(len(colum)):
    charlson(colum[k])
一直出现'numpy.int64' object has no attribute 'startswith',主要是因为'service_num'这列为数字,'service_unit'这一列有空值,无法使用pandas的内置的str函数

解决的办法就是在函数中直接将所有要遍历的列类型转换成str,函数修改成这个样子就可以了:

def charlson(day):
    patient[day]=patient[day].astype(str)#将该列类型转换为str,列中的空值会被填补为nan
    for i in range(1776):
        for j in range(len(alist)):           
            if patient[day][i].startswith(alist[j]):
                last[i]+=2


你可能感兴趣的:(python数据分析,pandas)