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

使用df.pivot绘图时指定索引为X轴触发AttributeError问题

Fixing the AttributeError for target_logtime in Your Pivot Table

The Root Cause

When you ran df.pivot(index='target_logtime', columns='valuekey', values='value'), you set target_logtime as the row index of the resulting DataFrame (dfpiv2), not a regular data column. That's why dfpiv2.target_logtime throws an error—pandas doesn't recognize it as an attribute/column anymore.

Two Simple Fixes

Option 1: Convert the Index Back to a Column

Use reset_index() to turn the row index into a regular column. This makes target_logtime accessible just like your other columns:

import pandas as pd
df = pd.read_csv('******.csv')
# Add reset_index() here to move index to column
dfpiv2 = df.pivot(index='target_logtime', columns='valuekey', values='value').reset_index()
import plotly
plotly.tools.set_credentials_file(username='********', api_key='*******')
import plotly.plotly as py
import plotly.graph_objs as go

# Now you can access target_logtime normally
x = dfpiv2.target_logtime
y1 = dfpiv2[5575]

trace1 = go.Scatter(
    x = x,
    y = y1,
    mode = 'lines',
    name = '5575'
)
data = [trace1]
py.iplot(data, filename='basiclinetest')

Option 2: Use the Index Directly for the X-Axis

Since the index is your target_logtime data, you can skip converting it to a column and just use dfpiv2.index directly. This is more efficient if you don't need target_logtime as a column for other operations:

import pandas as pd
df = pd.read_csv('******.csv')
dfpiv2 = df.pivot(index='target_logtime', columns='valuekey', values='value')
import plotly
plotly.tools.set_credentials_file(username='********', api_key='*******')
import plotly.plotly as py
import plotly.graph_objs as go

# Use the index directly as your x-values
x = dfpiv2.index
y1 = dfpiv2[5575]

trace1 = go.Scatter(
    x = x,
    y = y1,
    mode = 'lines',
    name = '5575'
)
data = [trace1]
py.iplot(data, filename='basiclinetest')

Either approach will resolve the AttributeError and let you plot your time vs temperature data correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:47:25