简体   繁体   English

用.tolist()生成的熊猫str.split产生了一个浮点数?

[英]pandas str.split with .tolist() produced a float?

I have a hard time bug fixing my code which worked fine in testing on a small subset of the entire data. 我遇到了一个很难修复的错误,该错误代码可以在对整个数据的一小部分进行测试时很好地工作。 I could double check types to be sure, but the error message is already informative enough: The list I made ended up being a float. 我可以确定类型是否经过仔细检查,但是错误消息已经足够翔实了:我制作的列表最终是浮点数。 But how? 但是如何?

The last three lines which ran: 运行的最后三行:

diagnoses = all_treatments['DIAGNOS'].str.split(' ').tolist()
all_treatments = all_treatments.drop(['DIAGNOS','INDATUMA','date'], axis=1)
all_treatments['tobacco'] = tobacco(diagnoses)

The error: 错误:

Traceback (most recent call last):
 File "treatments2_noiopro.py", line 97, in <module>
   all_treatments['tobacco'] = tobacco(diagnoses)
 File "treatments2_noiopro.py", line 13, in tobacco
   for codes in codes_column]
TypeError: 'float' object is not iterable

FWIW, the function itself is: FWIW,函数本身是:

def tobacco(codes_column):
    return [any('C30' <= code < 'C40' or 
                'F17' <= code <'F18'
                for code in codes) if codes else False
            for codes in codes_column]

I am using versions pandas 0.16.2 np19py26_0, iopro 1.7.1 np19py27_p0, and python 2.7.10 0 under Linux. 我在Linux下使用的版本是pandas 0.16.2 np19py26_0,iopro 1.7.1 np19py27_p0和python 2.7.10 0。

You can use str.split on the series and apply a function to the result: 您可以在序列上使用str.split并将一个函数应用于结果:

def tobacco(codes):
    return any(['C30' <= code < 'C40' or 'F17' <= code <'F18' for code in codes])

data = [('C35 C50'), ('C36'), ('C37'), ('C50 C51'), ('F1 F2'), ('F17'), ('F3 F17'), ('')]
df = pd.DataFrame(data=data, columns=['DIAGNOS'])

df

    DIAGNOS
0   C35 C50
1   C36
2   C37
3   C50 C51
4   F1 F2
5   F17
6   F3 F17
7   

df.DIAGNOS.str.split(' ').apply(tobacco)

0     True
1     True
2     True
3    False
4    False
5     True
6     True
7    False
dtype: bool

edit: 编辑:

Seems like using str.contains is significantly faster than both methods. 似乎使用str.contains明显比这两种方法都快。

tobacco_codes = '|'.join(["C{}".format(i) for i in range(30, 40)] + ["F17"])

data = [('C35 C50'), ('C36'), ('C37'), ('C50 C51'), ('F1 F2'), ('F17'), ('F3 F17'), ('C3')]
df = pd.DataFrame(data=data, columns=['DIAGNOS'])

df.DIAGNOS.str.contains(tobacco_codes)

I guess diagnoses is a generator and since you drop something in line 2 of your code this changes the generator. 我猜诊断是一个生成器,由于您在代码的第2行中放了一些东西,因此更改了生成器。 I can't test anything right now, but let me know if it works when commenting line 2 of your code. 我目前无法测试任何内容,但是在注释您的代码的第2行时让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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