首页 > 解决方案 > 处理 .csv 文件:错误:只能将整数标量数组转换为标量索引

问题描述

在此处输入图像描述Python 机器学习处理 .csv 文件:错误:只能将整数标量数组转换为标量索引

import pandas as pd
df = pd.read_csv('EURGBP.csv')
df.columns = [['date', 'open', 'high', 'low', 'close', 'volume']]
df.date = pd.to_datetime(df.date,format='%d.%m.%Y  %H:%M:%S.%f')
df = df.set_index(df.date)
print(df.head())

Error: only integer scalar arrays can be converted to a scalar index

标签: pythonpandascsvdataframeindexing

解决方案


问题是您将嵌套列表传递给列名,因此创建'broken'了一层MultiIndex,需要删除外部[]

df.columns = ['date', 'open', 'high', 'low', 'close', 'volume']

推荐阅读