python - 使用 Python/Pandas 按行获取列内容的值

标签 python excel pandas python-3.7

我已经用 python pandas 确定了我需要在我的 excel 文件中获取的行号;使用以下内容:

 row_numberd1 = df[df['Member Name'].str.contains(NameUp)].index.min()
 row_numberd12 = df[df['Member Address Line 3'].str.contains(Zip)].index.min()

当我打印时,行号显示:

 print(row_numberd1)
 print(row_numberd12)

现在,我只是想知道如何使用行号;获得不同的列值。因此,按行号获取列值。

例如,在下面的示例中:

   Age      City
1   34    Sydney
2   30     Delhi
3   16  New York

如何使用行号假设第 3 行,列“City”以获得“New York”值?

最佳答案

.loc 用于基于标签的索引

df.loc[3, 'City']
#'New_York'

# For a single value use `.at`
df.at[3, 'City']
#'New_York'

iloc 用于基于索引的选择

df.iloc[2, 1]
#'New_York'

# For a single value use `.iat`
df.iat[2, 1]
#'New_York'

iloc + get_loc 两者的结合

(即“城市”中的第三个值)

df.iloc[2, df.columns.get_loc('City')]
#'New_York'

关于python - 使用 Python/Pandas 按行获取列内容的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57258992/

相关文章:

Python 守护进程使用相同的对象列表

python - python mock __init__() 返回伪类的正确方法

java - 使用java将下载保存到浏览器下载而不是系统下载

python - 在 matplotlib 绘图中使用 cos 函数

excel - 如何在vba(excel)中使用multichar字符串作为分隔符来拆分字符串

excel - 如何从Excel列字母中获取列号(或索引)

pandas - 使用多索引数据框的问题

python - Pandas read_csv 日期时间错误

Python:根据 Pandas 数据框中某些行出现的两列(变量)获取频率计数

python - 存储事件后,异步事件等待函数不会继续