Python lambda:TypeError:类型' float'的参数是不可迭代的 [英] Python lambda: TypeError: argument of type 'float' is not iterable

查看:59
本文介绍了Python lambda:TypeError:类型' float'的参数是不可迭代的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将 df ['matrix'] 中的信息提取到四个新列中. df ['matrix'] 看起来像这样:

I was trying to extract information from df['matrix'] into four new columns. The df['matrix'] look like this:

  id       matrix
   0  {'status': 'ZERO_RESULTS'}
   1  {'distance': {'text': '3,899 km', 'value': 3898595}, 'duration': {'text': '1 day 13 hours', 'value': 133445}, 'status': 'OK'}
   2  {'distance': {'text': '2,065 km', 'value': 2065157}, 'duration': {'text': '20 hours 7 mins', 'value': 72393}, 'status': 'OK'}

我的代码:

df['dist_value'] = df['matrix'].apply(lambda x: round((x['distance']['value']) / 1000) if "status" not in x else None)
df['dist_text'] = df['matrix'].apply(lambda x: x['distance']['text'] if "status" not in x else None)

df['duration_value'] = df['matrix'].apply(lambda x: float("%.2f" %((x['duration']['value'])/60/60)) if "status" not in x else None)
df['duration_text'] = df['matrix'].apply(lambda x: x['duration']['text'] if "status" not in x else None)

我收到以下错误:

df['dist_value'] = df['matrix'].apply(lambda x: round((x['distance']['value']) / 1000) if "status" not in x else None)
TypeError: argument of type 'float' is not iterable

推荐答案

由于您要检查状态"是否在x中,因此有时x不是字符串.您可以使用str(x)将x转换为字符串.

Since you are checking if "status" is in x and it looks like sometimes x is not a string. You can cast your x to string with str(x).

希望它能解决您的问题.

Hope it will solve your problem.

这篇关于Python lambda:TypeError:类型' float'的参数是不可迭代的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆