访问Pandas / Python中选定单元格的索引/行/列
我应该把具有不平凡function的Excel电子表格转换成一个Web应用程序。 我已经决定和pandas一起工作,我想知道怎样处理这个问题。
我需要做的事情之一是允许人们input一个特定的数字(我们称之为lot_number
),并访问同一行中的其他值。
例如,如果lot_number = 3
,则不同列中的address_value
将为555 Something Street。 所有的lot_numbers
和address_value
条目是不同的。
我的问题是:我怎样才能访问地址值使用pandas根据lot_number
?
更新
使用iloc。 示例如下:
df.iloc[row, column] # accepts ints
这将使您可以访问该列和行。
更新
所以我们find行索引,然后做我原来的build议,并获得整个行。
row = df.loc[df['address_value']==lot_number].index[0] df.iloc[row]
通过使用lookup
df.lookup(df[df['lot_number']==3].index,['address_value']) Out[211]: array(['foo'], dtype=object)
数据input
df Out[212]: address_value lot_number 0 foo 3 1 bar 2 2 blah 1 df = pd.DataFrame({'lot_number':[3,2,1], 'address_value':['foo','bar','blah']})
编辑
df = pd.DataFrame({'lot_number':[3,3,1], 'address_value':['foo','bar','blah']}) df.lookup(df[df['lot_number']==3].index,['address_value']*len(df[df['lot_number']==3].index)) Out[223]: array(['foo', 'bar'], dtype=object)