Python TypeError: Index(...) must be called with a collection of some kind, None was passed

时间:2025-02-11 13:49:32

报错的意思就是
索引(…)必须用某种集合调用,但未传递任何集合,比如下面这种情况

columns=['v1', 'v2', 'v3', 'v4', 'v5',  'v6', 'price']
df_test=columns.remove('price')

原因是remove是没有返回值的,改成下面即可

columns.remove('price')
df_test.columns=columns

还有一种情况
Index(…) must be called with a collection of some kind, ‘xxx’ was passed
返回的值不是集合

data=[1,2,3,4]
pd.DataFrame(data,columns='value')

改成

data=[1,2,3,4]
pd.DataFrame(data,columns=['value'])

总之Index(…) must be called with a collection of some kind
就是索引(…)必须用某种集合调用,给index传参时,要传递列表。

感兴趣的可以看看pandas的源码