You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将public_client返回的Generator转为Pandas DataFrame并解决报错

Solution to Convert Generator to Pandas DataFrame (Fixing AttributeError)

Yep, that stray 'message' string at the end of your list is exactly what's causing the AttributeError! Here's why and how to fix it:

Why the Error Happens

pd.DataFrame.from_records() expects every element in your input to be a dictionary (or an object with a .keys() method) so it can map the keys to DataFrame columns. That random string 'message' doesn't have a .keys() attribute—hence the error you're seeing.

Fix: Filter Out Non-Dictionary Elements

You can solve this by filtering out any elements in the generator that aren't dictionaries before converting to a DataFrame. This is efficient to do directly with the generator (no need to convert it to a list first, which saves memory):

import pandas as pd

# Filter the generator to keep only dictionary items
filtered_trades = (item for item in data if isinstance(item, dict))
df = pd.DataFrame.from_records(filtered_trades)

If you prefer to first convert the generator to a list (to inspect it easily), you can filter the list instead:

import pandas as pd

# Convert generator to list first
trade_list = list(data)
# Filter out non-dictionary elements
filtered_trades = [item for item in trade_list if isinstance(item, dict)]
df = pd.DataFrame(filtered_trades)

Bonus: Inspect the Mysterious 'message'

If you want to check what that 'message' string contains (it might be a warning or error from the API), you can extract it separately:

trade_list = list(data)
# Split into valid trade dictionaries and other messages
valid_trades = [item for item in trade_list if isinstance(item, dict)]
api_messages = [item for item in trade_list if not isinstance(item, dict)]

# Print the messages to debug
print("API Messages:", api_messages)

# Create DataFrame from valid trades
df = pd.DataFrame(valid_trades)

内容的提问来源于stack exchange,提问作者spotnag

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:18:52