从 Pandas/Python 中的选定单元格访问索引/行/列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46496393/
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
Accessing the index/row/column from a selected cell in Pandas/Python
提问by rustyshackleford
I'm supposed to be turning an excel spreadsheet with nontrivial functions into a web application. I have decided to work with Pandas, and I'm wondering what the best way to approach this is.
我应该将具有重要功能的 Excel 电子表格转换为 Web 应用程序。我决定与 Pandas 合作,我想知道解决这个问题的最佳方法是什么。
One of the things I need to do is allow people to input a certain number (we'll call this lot_number
) and access other values in the same row.
我需要做的一件事是允许人们输入某个数字(我们称之为lot_number
)并访问同一行中的其他值。
For example, if lot_number = 3
, address_value
in a different column will be 555 Something Street. All of the lot_numbers
and address_value
entries are different.
例如,如果lot_number = 3
,address_value
在不同的列中将是 555 Something Street。所有的lot_numbers
和address_value
条目都不同。
My question is this: how can I access address_value
using pandas depending on lot_number
?
我的问题是:我怎样才能访问address_value
使用Pandas取决于lot_number
?
回答by Adders
Updated
更新
Use iloc. Example as follows:
使用 iloc。示例如下:
df.iloc[row, column] # accepts ints
This will give you access to that column and row.
这将使您可以访问该列和行。
Updated
更新
So we find the row index and then do my original suggestion and get the entire row.
所以我们找到行索引,然后按照我原来的建议得到整行。
row = df.loc[df['address_value']==lot_number].index[0]
df.iloc[row]
回答by YOBEN_S
By using lookup
通过使用 lookup
df.lookup(df[df['lot_number']==3].index,['address_value'])
Out[211]: array(['foo'], dtype=object)
Data 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']})
EDIT
编辑
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)