代码:-
import pandas as pd
df = pd.read_csv(r'C:UsersDesktopDScars.csv',header=0)
df.columns
O/p:-
Index(['Car;MPG;Cylinders;Displacement;Horsepower;Weight;Acceleration;Model;Origin'], dtype='object')
代码:-
df = df.set_index('Horsepower')
df.head
ERROR :- "None of ['Horsepower'] are in the columns"
我正在使用set_index将DataFrame的索引更改为"马力"。我不知道哪里出了问题。
看起来您的csv有分号;
作为分隔符。
默认情况下,pd.read_csv
使用逗号(,
(作为分隔符,对于其他所有分隔符,您需要显式传递相同的:
df = pd.read_csv(r'C:UsersDesktopDScars.csv',header=0, sep=';')