访问 Pandas 数据透视表中元素的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35293696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
the correct way to access elements in a pandas pivot table
提问by yoshiserry
I have been trying to access elements from the following pivot table using the pandas dataframe slicing .IX notation. however I am getting errors:
我一直在尝试使用 Pandas 数据帧切片 .IX 表示法访问以下数据透视表中的元素。但是我收到错误:
No Key.
没有钥匙。
pivot = c.pivot("date","stock_name","close").resample("A",how="ohlc")
pt = pd.DataFrame(pivot,index=pivot.index.year)
pt
What is the correct way to slice out only one or more rows and or columns from a pandas pivot table?
从 Pandas 数据透视表中只切出一行或多行和/或列的正确方法是什么?
For example if I just want the prices for the year 2016
for Billabong
?
例如,如果我只是想这一年的价格2016
为Billabong
?
pivot["2016-12-31"]["BBG"]
采纳答案by jezrael
print c
date stock_name close
0 2012-08-31 ibm 1
1 2013-08-31 aapl 1
2 2014-08-31 goog 1
3 2015-08-31 bhp 1
4 2016-08-31 bhp 1
pivot = c.pivot("date","stock_name","close").resample("A",how="ohlc")
print pivot
aapl bhp goog ibm \
open high low close open high low close open high low close open
date
2012-12-31 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 1
2013-12-31 1 1 1 1 NaN NaN NaN NaN NaN NaN NaN NaN NaN
2014-12-31 NaN NaN NaN NaN NaN NaN NaN NaN 1 1 1 1 NaN
2015-12-31 NaN NaN NaN NaN 1 1 1 1 NaN NaN NaN NaN NaN
2016-12-31 NaN NaN NaN NaN 1 1 1 1 NaN NaN NaN NaN NaN
high low close
date
2012-12-31 1 1 1
2013-12-31 NaN NaN NaN
2014-12-31 NaN NaN NaN
2015-12-31 NaN NaN NaN
2016-12-31 NaN NaN NaN
print pivot.loc["2014", ('goog', slice(None))]
goog
open high low close
date
2014-12-31 1 1 1 1