Pandas透视表筛选报错AttributeError:DataFrame无Supply属性求解
问题分析与解决方法
需求说明
从名为df_IBIAS_mode1_FUN的DataFrame中筛选Supply[V]=2.5的数据,生成透视表:
- 设置
index='Device_ID' - 设置
columns='Temp(deg)' - 设置
values='ibias_post_trim[MHz]'
错误代码
df_IBIAS_mode1_FUN[df_IBIAS_mode1_FUN.Supply[V] == 2.5].pivot_table(index='Device_ID', columns='Temp(deg)', values='ibias_post_trim[MHz')
错误信息
AttributeError: 'DataFrame' object has no attribute 'Supply'
错误原因及修正
列名访问方式错误
你的列名是Supply[V],包含特殊字符[],无法通过.(点语法)访问DataFrame的列(点语法仅支持符合变量命名规则的列名)。必须用方括号[]包裹完整列名来访问。values参数字符串语法错误
'ibias_post_trim[MHz'缺少闭合的],正确写法应为'ibias_post_trim[MHz]'。
修正后的代码
# 分步写法,更易读 filtered_data = df_IBIAS_mode1_FUN[df_IBIAS_mode1_FUN['Supply[V]'] == 2.5] pivot_table_result = filtered_data.pivot_table( index='Device_ID', columns='Temp(deg)', values='ibias_post_trim[MHz]' ) # 或者合并为一行 pivot_table_result = df_IBIAS_mode1_FUN[df_IBIAS_mode1_FUN['Supply[V]'] == 2.5].pivot_table( index='Device_ID', columns='Temp(deg)', values='ibias_post_trim[MHz]' )
内容的提问来源于stack exchange,提问作者Confused
相关产品推荐
相关产品推荐

