Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 读取csv文件中的索引列时出现问题_Python_Pandas_Csv - Fatal编程技术网

Python 读取csv文件中的索引列时出现问题

Python 读取csv文件中的索引列时出现问题,python,pandas,csv,Python,Pandas,Csv,我正在读熊猫的csv文件。当我打印df.shape时,它显示正确的行数和列数,但当我尝试删除某些列时,它显示错误,因为列名未定义。这是我的密码: df = pd.read_csv('Weather_data.csv',sep=',',header=0,parse_dates=["datetime_utc"]) print(df.columns) print(df.head()) print(df.shape) 它打印为(98913,20) 当我试图删除列时,它会显示错误 d

我正在读熊猫的csv文件。当我打印df.shape时,它显示正确的行数和列数,但当我尝试删除某些列时,它显示错误,因为列名未定义。这是我的密码:

  df = pd.read_csv('Weather_data.csv',sep=',',header=0,parse_dates=["datetime_utc"])
  print(df.columns)
  print(df.head())
  print(df.shape)
它打印为(98913,20) 当我试图删除列时,它会显示错误

  df.drop(columns=['_fog','_hail','_rain','_snow','_thunder','_tornado','_wdire','_windchillm','_wgustm'],axis=1,inplace=True])
这是引发的关键错误:

KeyError: "['_fog' '_hail' '_rain' '_snow' '_thunder' '_tornado' '_wdire'\n '_windchillm' '_wgustm'] not found in axis"
请告诉我为什么会这样 df.columns的结果:

Index(['datetime_utc', ' _conds', ' _dewptm', ' _fog', ' _hail',
       ' _heatindexm', ' _hum', ' _precipm', ' _pressurem', ' _rain', '_snow',
       ' _tempm', ' _thunder', ' _tornado', ' _vism', ' _wdird', ' _wdire',
       ' _wgustm', ' _windchillm', ' _wspdm'],
      dtype='object')

同时指定标签和索引或列将引发ValueError。 因此,对于例如,使用“df.drop(['B','C'],axis=1)”或
drop(列=['B','C'])。希望这将有帮助

您能显示
打印(df.columns)的结果吗
是的,我已经在上面上传了。您看到问题了,您试图删除的列不在
df
中。您的列名有前导空格<代码>''u fog'!='_雾'。首先在列名上尝试
strip()
,或者在
drop()中的
columns=
中添加前导空格。
Plz将结果图像与我前面提到的代码一起共享