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

将SQLite表加载到Pandas DataFrame时触发AttributeError求助

Fixing AttributeError: 'list' object has no attribute 'name' When Loading SQLite Data into Pandas

Hey there! Let’s break down why you’re running into this error and how to fix it quickly.

The AttributeError: 'list' object has no attribute 'name' typically pops up when Pandas expects a data source that includes column metadata (like a database cursor or SQL query result), but you’re passing a raw list of tuples without any column context. Here are two reliable solutions:

Skip manually fetching tuples entirely—use Pandas’ built-in functions to read directly from your SQLite connection. This automatically captures column names and structures the data correctly:

import sqlite3
import pandas as pd

# Connect to your SQLite database
conn = sqlite3.connect("your_database_file.db")

# Read data directly into a DataFrame with column names intact
df = pd.read_sql_query("SELECT * FROM your_target_table", conn)

# Clean up the connection
conn.close()

This method avoids the hassle of manually handling tuples and column names, and it’s less error-prone.

Solution 2: Manually Add Column Names to Your Tuple List

If you already have the list of tuples (e.g., from cursor.fetchall()), you just need to extract the column names from the cursor’s metadata and pass them to the DataFrame constructor:

import sqlite3
import pandas as pd

conn = sqlite3.connect("your_database_file.db")
cursor = conn.cursor()

# Execute your query and fetch the raw tuple data
cursor.execute("SELECT * FROM your_target_table")
tuple_data = cursor.fetchall()

# Extract column names from the cursor description
column_names = [col[0] for col in cursor.description]

# Create the DataFrame with both data and column names
df = pd.DataFrame(tuple_data, columns=column_names)

# Clean up
cursor.close()
conn.close()

The key here is that cursor.description gives you metadata about each column (including its name), which Pandas needs to properly structure the DataFrame. Without this, Pandas might try to access internal attributes that don’t exist on a raw list, leading to the 'list' object has no attribute 'name' error.

Why Your Original Approach Failed

Chances are you were passing the raw tuple list to a function that expects a database cursor or SQL result set (like accidentally using pd.read_sql() with the list instead of a query and connection). Pandas’ internal code tries to access attributes like name to get column info, which doesn’t exist on a basic list of tuples.

If you tried other methods and hit new errors, feel free to share the specific error messages and code snippets—we can troubleshoot those too!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:32:52