我正在尝试一个简单的代码,但是它抛出了一个错误
import pandas as pd
df = pd.read_csv('AshokLeyland.csv')
print(df.head())
df = df[['Open Price','Close Price']]
错误
df = df[['Open Price','Close Price']]
Traceback (most recent call last):
File "<ipython-input-5-33b1f1145780>", line 1, in <module>
df = df[['Open Price','Close Price']]
File "C:\Users\gg\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1958, in __getitem__
return self._getitem_array(key)
File "C:\Users\gg\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2002, in _getitem_array
indexer = self.loc._convert_to_indexer(key, axis=1)
File "C:\Users\gg\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1231, in _convert_to_indexer
raise KeyError('%s not in index' % objarr[mask])
KeyError: "['Open Price' 'Close Price'] not in index"
我无法附加文件,但示例输出如下:-
print(df.head())
Date Symbol Series Open Price High Price Low Price \
0 06-Nov-18 ASHOKLEY EQ 121.35 122.15 118.70
1 05-Nov-18 ASHOKLEY EQ 117.65 121.85 116.20
2 02-Nov-18 ASHOKLEY EQ 114.40 118.45 113.80
3 01-Nov-18 ASHOKLEY EQ 115.50 116.85 112.35
4 31-Oct-18 ASHOKLEY EQ 115.00 115.90 112.20
print(df.columns.tolist())
['Date', ' Symbol', ' Series', ' Open Price', ' High Price', ' Low Price', ' Last Traded Price ', ' Close Price', ' Total Traded Quantity', ' Turnover (in Lakhs)']
不确定是什么问题。我只想用一些选定的列创建一个新的df。
答案 0 :(得分:0)
问题列名称中有多余的空格。
df = df[[' Open Price',' Close Price',' Low Price',' Close Price',' Total Traded Quantity']]
这解决了问题
答案 1 :(得分:-1)
import pandas as pd
df=pd.DataFrame([
['06-Nov-18','ASHOKLEY','EQ','121.35','122.15','118.70'],
['05-Nov-18','ASHOKLEY','EQ','117.65','121.85','116.20'],
['02-Nov-18','ASHOKLEY','EQ','114.40','118.45','113.80'],
['01-Nov-18','ASHOKLEY','EQ','115.50','116.85','112.35'],
['31-Oct-18','ASHOKLEY','EQ','115.00','115.90','112.20']],
columns=['Date','Symbol','Series','Open Price','High Price','Low Price'])
df
Date Symbol Series Open Price High Price Low Price
0 06-Nov-18 ASHOKLEY EQ 121.35 122.15 118.70
1 05-Nov-18 ASHOKLEY EQ 117.65 121.85 116.20
2 02-Nov-18 ASHOKLEY EQ 114.40 118.45 113.80
3 01-Nov-18 ASHOKLEY EQ 115.50 116.85 112.35
4 31-Oct-18 ASHOKLEY EQ 115.00 115.90 112.20
df[['Symbol','Open Price']]
Symbol Open Price
0 ASHOKLEY 121.35
1 ASHOKLEY 117.65
2 ASHOKLEY 114.40
3 ASHOKLEY 115.50
4 ASHOKLEY 115.00
它在这里工作。问题一定是在csv读取期间。注意每个列名称前的空格。字符串必须相同。