'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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值