【发布时间】:2014-07-04 09:24:29
【问题描述】:
我希望使用 pandas 数据框复制 numpy 数组的行为。我想传递一个索引和列名数组,并获取在相应索引和列名中找到的对象列表。
import pandas as pd
import numpy as np
在 numpy 中:
array=np.array(range(9)).reshape([3,3])
print array
print array[[0,1],[0,1]]
[[0 1 2]
[3 4 5]
[6 7 8]]
[0 4]
在熊猫中:
prng = pd.period_range('1/1/2011', '1/1/2013', freq='A')
df=pd.DataFrame(array,index=prng)
print df
0 1 2
2011 0 1 2
2012 3 4 5
2013 6 7 8
df[[2011,2012],[0,1]]
预期输出:
[0 4]
我应该如何切片这个数据帧以使其返回与 numpy 相同的值?
【问题讨论】:
-
严格来说,这不是一个 (row,col) 索引数组,而是一个多维索引数组。如果我的理解是正确的,请相应地编辑标题。
标签: python numpy pandas dataframe slice