Python 如何从数据框中获取特定值,使用pandas绘制图形
我想能够图形的具体日期为x值和大小为y值时,大小是24022和类型是出价。我尝试了以下方法:Python 如何从数据框中获取特定值,使用pandas绘制图形,python,pandas,dataframe,Python,Pandas,Dataframe,我想能够图形的具体日期为x值和大小为y值时,大小是24022和类型是出价。我尝试了以下方法: 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.rea
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"]
这应该有效。请将所有数据包括在问题中,而不是作为图像。这使得任何想要重现错误的人都要做很多工作……我不是说,你应该包括图像,而是说你应该键入示例数据,以便我们可以测试它;)对于任何想帮助你的人来说,图像总是让人恼火,因为我们必须输入数据……我明白,我输入了示例数据。只是为了将来,它让任何想帮助你的人都更容易:)你也尝试过用
现在[“size”]
替换“size”
和现在[“type”]
替换“type”
?我认为你应该使用和