我希望能够使用 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]
在Pandas中:
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相同的结果?