python - 尝试扩展行数据并转换为 DataFrame,出现此错误 : AttributeError: 'float' object has no attribute 'keys'

标签 python pandas list dataframe cryptocurrency

运行下面的函数时,我遇到以下错误: AttributeError:“float”对象没有属性“keys”

每行包含以下数据:

{'volume': '8579152.41', 'price_change': '-0.00211948', 'price_change_pct': '-0.0016', 'volume_change': '445336.65', 'volume_change_pct': '0.0548', 'market_cap_change': '-2813342.71', 'market_cap_change_pct': '-0.0016'}

def adj_col(data,column_name): # Function to expand columns open up brace brackets and then converts it to a DataFrame
    convert = data.pop(column_name).values.tolist()
    converted = pd.DataFrame(convert)
    return converted

我的目标是将数据扩展为一列,完成后将其转换为 DataFrame。

我做错了什么?

最佳答案

列中某处可能有 NaN 值。此示例将过滤掉 NaN 值:

convert = df.pop("Values").values.tolist()
converted = pd.DataFrame([v for v in x if pd.notna(v)])  # <-- filter out the NaN values
print(converted)

关于python - 尝试扩展行数据并转换为 DataFrame,出现此错误 : AttributeError: 'float' object has no attribute 'keys' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67549428/

相关文章:

python - 如何检查python中是否存在列表?

json - 将文本转换为 python 列表

Python请求,更改IP地址

python - Pyserial 没有从 Arduino 获得新的值(value)

python - 如何用单个 numpy 数组操作替换这个三重 For 循环?

python - 重新映射数据框中的值,其中键是一列,要替换的值是另一列

Python - Pandas 数据帧连接内存中的气球

具有线程支持的python网络爬虫

python - pandas 中的 pd.concat 出现 TypeError : cannot concatenate object of type '<class ' str'>'; only Series and DataFrame objs are valid

c# - 在 F# 中使用 SqlDataReader