简体   繁体   English

如何对数据框中的值列表进行赋值? 获取错误“浮动”对象不可下标

[英]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.

相关问题 错误:“浮动”object 不可下标 - Error: 'float' object is not subscriptable 从浮点值列表创建DataFrame - Creating a DataFrame from a list of float values 从列表中减去值,收到“生成器对象不可下标”错误 - Subtracting values from lists, received "generator object is not subscriptable" error 如何正确地为 Sympy Matrix 的元素赋值,解决 TypeError: 'Zero' object is not iterable or 'Float' object is not subscriptable - How correctly assign values to elements of Sympy Matrix, solving TypeError: 'Zero' object is not iterable or 'Float' object is not subscriptable 'float' 对象不可下标 - 列表编码 - 'float' object is not subscriptable - List Coding 为什么我得到一个“浮动”object is not subscriptable 错误 - Why am i getting a 'float' object is not subscriptable error 遇到错误:“浮动”对象不可下标 - Running into error: 'float' object is not subscriptable 不正确的“浮动 object 不可下标”错误 - Incorrect “float object is not subscriptable” Error Python 错误:“'float' 对象不可下标” - Python error: " 'float' object is not subscriptable" 尝试检查列表列表中的值是否小于零时,无法获得错误的“ float”对象 - Getting error 'float' object is not iterable when trying to check if values in the list of a list are less than zero
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM