Python lambda:TypeError:类型为';浮动';这是不可容忍的

Python lambda:TypeError:类型为';浮动';这是不可容忍的,python,pandas,lambda,Python,Pandas,Lambda,我试图将df['matrix']中的信息提取到四个新列中。df['matrix']如下所示: 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': {

我试图将
df['matrix']
中的信息提取到四个新列中。
df['matrix']
如下所示:

  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
因为您正在检查“status”是否在x中,有时x看起来不是字符串。您可以使用str(x)将x转换为字符串


希望它能解决您的问题。

似乎
x
是一个浮点,而不是一个可数(因此
在x
中)失败
x
df['matrix']
的一个元素,因此猜测一个元素的类型不同。您确定
'matrix'
列中的元素不再是字典了吗?我无法用上面给出的数据重现您的问题。考虑到前面的评论,您可能已经修改了中间的数据,导致它不再是一个dict。尝试查看例如
df['matrix'].apply(lambda x:x.keys())
是否有效。x.keys()中没有
“status”(
解决错误吗?在数据中,所有行中都有
“status”
。所以仅仅检查密钥中的状态是行不通的