简体   繁体   English

如何从数据框中获取特定值,使用 pandas 绘制图形

[英]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.

相关问题 如何使用 python 从 pandas 数据帧中获取特定值? - How to get specific values in from a pandas data frame using python? 如何使用 pandas 根据同一数据帧中另一列的条件获取列值的连续平均值 - How to get consecutive averages of the column values based on the condition from another column in the same data frame using pandas 如何使用多列中的值对pandas数据框进行排序? - How to sort pandas data frame using values from several columns? 如何替换熊猫数据框中单个行和特定列的值 - how to replace values of an individual row and specific columns in pandas data frame 如何根据特定行的列值查询 pandas 数据帧 - How to query on a pandas data frame based on column values of specific row 如何在特定索引处重复熊猫数据框列中的值? - How to repeat values in pandas data frame column at specific indices? 如何在特定的熊猫数据框列中查找值,然后将该行中的其他值存储在单独的变量中 - How to look for a value in a specific pandas data frame column and then store the other values from that row in separate variables 如何从 pandas 数据帧中的逗号分隔值计算以特定 substring 开头的字符串的出现次数? - How to count the occurrences of a string starts with a specific substring from comma separated values in a pandas data frame? Pandas:如何合并两个数据帧并使用第二个数据帧中的值填充 NaN 值 - Pandas: How to merge two data frames and fill NaN values using values from the second data frame 从Pandas数据框中获取具有每日数据的业务月末值 - Get business monthend values from Pandas data frame with daily data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM