python - 如何从数据框中获取特定值,使用熊猫绘制图表
问题描述
当大小为 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
解决方案
您的问题的答案是,它&
的优先级高于==
. 所以你需要做的就是把你的条件放在括号里:
x1 = now3.loc[(now3["size"] == 24022) & (now3["type"]=='BID'), "date"]
y1 = now3.loc[(now3["size"] == 24022) & (now3["type"]=='BID'), "size"]
这应该有效。
推荐阅读
- javascript - 卸载时清除 this.setState 任务的优雅方法,没有 this.mounted 模式?
- macos - 如何在文件添加到文件夹时重命名文件?
- c++ - 使用 QAudioInput 录制音频时无法读取字节
- php - 需要一种方法将实际使用 PHP 构建的测验的可用时间限制为 50 分钟
- r - 根据 shinydashboard 中的值创建动态 valueBox 颜色
- spring - 如何处理 zuul 中的 hystrix 回退?
- python - 如何使用 pdfminer 6 获取可编辑字段的页码
- sql - 删除重复的行集
- c# - 在 C# 中,for 循环是线性的吗?
- ios - CollectionViewCells 不会以编程方式出现 - Swift 5