[英]How to get specific values from data frame, using pandas to graph
I would like to be able to graph the specific date as x value and size as y value when the size is 24022 and the type is bid.当大小为 24022 且类型为出价时,我希望能够将特定日期绘制为 x 值,并将大小绘制为 y 值。 I tried the following:
我尝试了以下方法:
headers = ['ticker', 'size', 'price', 'unix','type','date']
dtypes = {'ticker': 'str', 'size': 'int', 'price': 'float', 'unix': 'float','type': 'str','date': 'str'}
parse_dates = ['date']
btcnow = pd.read_csv('new 1031-113.csv', header=None, names=headers, dtype=dtypes, parse_dates=parse_dates)
#btc105=pd.read_excel('11-3-11-5 data.xlsx',sheet_name="Sheet1",header=None)
#btc103=btc103.append(btc105,ignore_index = True)
now3 = pd.DataFrame(btcnow, columns=['size','date','type'])
now4 = pd.DataFrame(btcnow, columns=['date','price'])
x1 = now3.loc[now3["size"] == 24022 & now3["type"]=='BID', "date"]
y1 = now3.loc[now3["size"] == 24022 & now3["type"]=='BID', "size"]
But x1 and y1 give me an error: 'cannot compare a dtyped [object] array with a scalar of type [bool]' Here is what the now3 dataframe looks like:但是 x1 和 y1 给了我一个错误:“无法将 dtyped [object] 数组与 [bool] 类型的标量进行比较”下面是 now3 dataframe 的样子:
size date type
0 200 2019-10-31 15:06:00 ASK
1 11000 2019-10-31 15:06:00 ASK
2 1 2019-10-31 15:06:00 BID
3 1 2019-10-31 15:06:00 ASK
4 10000 2019-10-31 15:06:00 ASK
... ... ... ...
1048570 1575 2019-11-03 02:42:00 ASK
1048571 4 2019-11-03 02:42:00 BID
1048572 120 2019-11-03 02:42:00 ASK
1048573 4 2019-11-03 02:42:00 BID
1048574 7472 2019-11-03 02:42:00 ASK
The answer to your problem is, that &
has higher priority than ==
.您的问题的答案是,
&
的优先级高于==
。 So all you need to do is put your conditions in brackets:所以你需要做的就是把你的条件放在括号里:
x1 = now3.loc[(now3["size"] == 24022) & (now3["type"]=='BID'), "date"]
y1 = now3.loc[(now3["size"] == 24022) & (now3["type"]=='BID'), "size"]
This should work.这应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.