Python ';列表索引必须是整数或切片,而不是字符串';但它们实际上是整数

Python ';列表索引必须是整数或切片,而不是字符串';但它们实际上是整数,python,pandas,dataframe,series,Python,Pandas,Dataframe,Series,在问题标题中,我对以下代码有问题: for index in range(0, len(processed_df) - PERIOD, STEP): print(index) print(type(index)) x_samples = processed_df['x'].iloc[index:index + PERIOD] y_samples = processed_df['y'].iloc[index:index + PERIOD] z_samples

在问题标题中,我对以下代码有问题:

for index in range(0, len(processed_df) - PERIOD, STEP):
    print(index)
    print(type(index))
    x_samples = processed_df['x'].iloc[index:index + PERIOD]
    y_samples = processed_df['y'].iloc[index:index + PERIOD]
    z_samples = processed_df['z'].iloc[index:index + PERIOD]
    label = stats.mode(processed_df['activity'][index:index + PERIOD])[0][0]
    fragments.append([x_samples, y_samples, z_samples])
    fragments_labels.append(label)
打印输出:

0
<class 'int'>
在线:

x_samples = processed_df['x'].iloc[index:index + PERIOD]

processed\u-df
是一个列表,而不是数据帧。@Barmar
type(processed\u-df)
Out[3]:pandas.core.frame.dataframe
因此它已经是一个DFT消息
“列表索引必须是整数或片,而不是字符串”
指用于访问元素的索引,而不是元素本身。由于
processed_-df
是一个列表,因此放置
processed_-df['x']
意味着“给我索引'x'处的元素”,这在列表上下文中没有任何意义。好的,你是对的。如何将其作为数据帧访问?为什么在变量列表和控制台中它显示为数据帧,而在代码中(我在运行程序中打印了它)它是一个类型:list?
x_samples = processed_df['x'].iloc[index:index + PERIOD]