新手在这里,我的csv文件包含我想用UNIQUE float索引的行
"GOOGL @ 759.28"
"August 19 2016 Calls","","","","","","","","","","August 19 2016 Puts"
,"IV","Delta","Open Int","Vol","Change","Last","Bid","Ask","Strike","Bid","Ask","Last","Change","Vol","Open Int","Delta","IV"
,"85.67","0.971","1","0","0.00","233.45","257.30","261.60","500.00","0.00","3.60","0.05","0.00","0","2","-0.025","83.12"
,"83.91","0.971","0","0","0.00","0.00","252.20","256.60","505.00","0.00","0.50","0.00","0.00","0","0","-0.006","61.19"
,"82.16","0.970","0","0","0.00","0.00","247.30","251.60","510.00","0.00","4.00","0.81","0.00","0","1","-0.028","81.24"
,"46.25","0.999","0","0","0.00","0.00","242.30","246.70","515.00","0.00","0.55","0.00","0.00","0","0","-0.007","59.20"
,"44.85","0.999","0","0","0.00","0.00","237.40","241.60","520.00","0.05","0.60","0.13","0.00","0","11","-0.008","59.08"
,"43.47","0.999","0","0","0.00","0.00","232.40","236.60","525.00","0.00","0.60","0.00","0.00","0","0","-0.007","57.16"
,"42.08","0.999","0","0","0.00","0.00","227.40","231.60","530.00","0.00","0.65","0.10","0.00","0","18","-0.008","56.41"
我的代码:
#! /usr/local/bin/python
import numpy as np
import pandas as pd
import locale
locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' )
def read_csv_file(pathToFile="options.csv"):
return pd.read_csv(pathToFile, header=2,
dtype={"Strike":np.float}, index_col="Strike",
usecols=["Strike","Bid","Ask","Bid.1","Ask.1"],
thousands=',').reset_index()
def list_record_with_strike(strike, df):
for index, longCall in df.iterrows():
if strike == np.float(longCall['Strike']) :
print("Ask Price of strike: ", longCall['Strike'], "is ",longCall['Ask'])
def get_record_by_strike(strike, df ):
print(df[strike])
def main():
# data frame
df = read_csv_file()
list_record_with_strike(510,df)
get_record_by_strike(510,df)
if __name__ == '__main__':
main()
我得到了这个输出:
> Ask Price of strike: 510.0 is 251.6 Traceback (most recent call
> last): File "spikes/OptionsReader.py", line 32, in <module>
> main() File "spikes/OptionsReader.py", line 29, in main
> get_record_by_strike(510,df) File "spikes/OptionsReader.py", line 23, in get_record_by_strike
> print(df[strike]) File "//anaconda/lib/python3.5/site-packages/pandas/core/frame.py", line
> 1969, in __getitem__
> return self._getitem_column(key) File "//anaconda/lib/python3.5/site-packages/pandas/core/frame.py", line
> 1976, in _getitem_column
> return self._get_item_cache(key) File "//anaconda/lib/python3.5/site-packages/pandas/core/generic.py", line
> 1091, in _get_item_cache
> values = self._data.get(item) File "//anaconda/lib/python3.5/site-packages/pandas/core/internals.py",
> line 3211, in get
> loc = self.items.get_loc(item) File "//anaconda/lib/python3.5/site-packages/pandas/core/index.py", line
> 1759, in get_loc
> return self._engine.get_loc(key) File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:3979) File
> "pandas/index.pyx", line 157, in pandas.index.IndexEngine.get_loc
> (pandas/index.c:3843) File "pandas/hashtable.pyx", line 668, in
> pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12265)
> File "pandas/hashtable.pyx", line 676, in
> pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12216)
> KeyError: 510
现在,我知道我可以获得一个系列因为list_record_with_strike正确定位它。 (但我经历了全部事情)但由于某种原因(我认为微不足道,但我不知道)我无法通过其索引直接得到记录......
答案 0 :(得分:0)
试试这个:
Attempted relative import beyond toplevel package (/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py:1552)
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~polly-chat/1.394430414829237783/main.py", line 48, in post
receive_message(messaging_event)
File "/base/data/home/apps/s~polly-chat/1.394430414829237783/messaging/handler.py", line 39, in receive_message
intent_picker.respond_to_postback(messaging_event)
File "/base/data/home/apps/s~polly-chat/1.394430414829237783/messaging/intent_picker.py", line 71, in respond_to_postback
intent = importlib.import_module('intents.register_user')
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/base/data/home/apps/s~polly-chat/1.394430414829237783/intents/register_user.py", line 1, in <module>
from ..models import messenger_user
ValueError: Attempted relative import beyond toplevel package
答案 1 :(得分:0)
好的,我&#34; RTFM&#34; (http://pandas.pydata.org/pandas-docs/stable/10min.html)并注意到我必须明确说我要比较列中的值&#39; Strike&#39;这将给我系列。
这是我的新代码:
#! /usr/local/bin/python
import numpy as np
import pandas as pd
import locale
locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' )
def read_csv_file(pathToFile="options.csv"):
return pd.read_csv(pathToFile, header=2,
dtype={"Strike":np.float}, index_col="Strike",
usecols=["Strike","Bid","Ask","Bid.1","Ask.1"],
thousands=',').reset_index()
def list_record_with_strike(strike, df):
for index, longCall in df.iterrows():
if strike == np.float(longCall['Strike']) :
print("Ask Price of strike: ", longCall['Strike'], "is ", longCall['Ask'])
def get_record_by_strike(strike, df ):
series = df[df.Strike == strike]
print("Ask price for strike: ", series.iat[0,0], " is " , series.iat[0,2])
def main():
# data frame
df = read_csv_file()
get_record_by_strike(510.0,df)
if __name__ == '__main__':
main()
观察我通过df [df.Strike == strike]得到一个系列,它返回包含我想要的数据的整行。 然后,我使用iat [x,y],x always = 0,并获取特定列的值。
如果有更多的pythonic / pandonic方式,请发布。