遍历熊猫系列给

时间:2020-06-12 17:43:35

标签: python pandas dataframe

基本上我有这个pd.series:

0         03/25/93 Total time of visit (in minutes):\n
1                       6/18/85 Primary Care Doctor:\n
2    sshe plans to move as of 7/8/71 In-Home Servic...
3                7 on 9/27/75 Audit C Score Current:\n
4    2/6/96 sleep studyPain Treatment Pain Level (N...

当我尝试通过循环对其进行迭代时:

  for i,row in enumerate(df):
    d= row[i].len()

或者这个:

    for row in df:
        d= row.len()

我收到此错误:

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

当我尝试其他操作(如findall等)时,也会收到此错误消息。

希望有人能启发我! 谢谢。

1 个答案:

答案 0 :(得分:1)

您必须使用.str来访问Series中的字符串功能,并且不需要遍历每一行。

这可以;

df['str_len'] = df['str_column'].str.len()

pandas中,.findall.str.contains,它返回一个布尔索引器。

像这样使用它;

substring = 'hello'

df[df['str_column'].str.contains(substring)]