[英]How to values a list of values from a dataframe? getting error 'float' object is not subscriptable
I'm having trouble getting values from a dataframe I created from open weather data.我无法从我从开放天气数据创建的数据框中获取值。
0 NaN
1 {'lon': -17.1, 'lat': 32.67}
2 {'lon': -73.36, 'lat': 41.14}
3 {'lon': 18.42, 'lat': -33.93}
4 {'lon': -53.81, 'lat': -29.68}
...
617 {'lon': -88.69, 'lat': 41.99}
618 NaN
619 {'lon': -108.22, 'lat': 36.73}
620 {'lon': -73.58, 'lat': 40.66}
621 {'lon': 28.92, 'lat': 61.87}
Name: coord, Length: 622, dtype: object
I am attempting to get a list including only lat values.我正在尝试获取一个仅包含 lat 值的列表。 My initial thought has been to run a loop and append a list however I'm getting the error "TypeError: 'float' object is not subscriptable"
我最初的想法是运行一个循环并附加一个列表,但是我收到错误“TypeError:'float' object is not subscriptable”
here is my code:这是我的代码:
lat = []
for i in coord:
lat.append(i['lat'])
desired output:所需的输出:
[32.67, 41.14, -33.93, -29.68, ..., 41.99, NaN, 36.73, 40.66, 61.87]
Some of the rows of your dataframe just contain NaN
, not a dictionary.数据帧的某些行仅包含
NaN
,而不是字典。 You need to check for that.你需要检查一下。
import math
lat = []
for i in coord:
if math.isnan(i):
lat.append(NaN)
else:
lat.append(i['lat']
I'm not sure why you're putting dictionaries in a dataframe, it defeats the purpose of using pandas.我不确定您为什么将字典放入数据框中,这违背了使用 Pandas 的目的。 You should make them separate columns in the df.
您应该使它们在 df 中分开列。 Then you would be able to use:
然后你就可以使用:
lat = coords['lat'].tolist()
This should do the trick for you:这应该对你有用:
lat=coord.str["lat"]
lon=coord.str["lon"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.