我想要一个简单的方法来访问指数相对于给定的指数在熊猫DataFrame
。请参考下面其中绘制一个类似于一个numpy
阵列的代码:熊猫数据帧相对索引
import numpy as np
import pandas as pd
# let's make a simple 2d matrix like array
na_a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
print na_a
print na_a[1][2]
print na_a[1+1][2] # here I want to print the next value in the same column so I iterate the row by 1
# now let's put this array into a pandas dataframe
df_a = pd.DataFrame(na_a,index = ['1','2','3','4'], columns = ['A','B','C','D'])
print df_a
print df_a['C']['2']
# now how do I easily iterate the row by 1 to print the next value in the same column?
这里是输出:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
7
11
A B C D
1 1 2 3 4
2 5 6 7 8
3 9 10 11 12
4 13 14 15 16
7
这应用于一般相对索引是可能的(不沿一个方向仅仅1 )。
Pandas背后的一个想法是能够轻松地对数据执行批量操作并避免迭代的需要。你能否详细说明你想在更高层面上完成什么? – Windstorm1981
为什么不使用基于整数的索引,即'iloc [2,2]'? – Alexander
在最高级别上,这是一个缺点和十字架游戏(连续5次)。我决定在一个DataFrame中保存板子(主要是因为我是python的新手)。我的AI对手现在可以通过查找连续X的链来找到潜在的威胁。假设三个X的链条在F5,F6,F7。我的约定给出链的地址为F7,它的长度为3.如果AI想要放下O来阻止链,它必须到达地址F7,并将数字索引迭代1到F8。 –