首页 > 解决方案 > 如何从数据框中获取特定值,使用熊猫绘制图表

问题描述

当大小为 24022 且类型为出价时,我希望能够将特定日期绘制为 x 值,将大小绘制为 y 值。我尝试了以下方法:

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"]

但是 x1 和 y1 给了我一个错误:'无法将 dtyped [object] 数组与 [bool] 类型的标量进行比较' 这是 now3 数据框的样子:

          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

标签: pythonpandasdataframe

解决方案


您的问题的答案是,它&的优先级高于==. 所以你需要做的就是把你的条件放在括号里:

x1 = now3.loc[(now3["size"] == 24022) & (now3["type"]=='BID'), "date"]
y1 = now3.loc[(now3["size"] == 24022) & (now3["type"]=='BID'), "size"]

这应该有效。


推荐阅读