【问题标题】:Get value of column content by row with Python/Pandas使用 Python/Pandas 逐行获取列内容的值
【发布时间】:2019-12-07 02:21:07
【问题描述】:

我已经用 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

如何使用行号; 假设Row 3,列'City'得到'New York'值?

【问题讨论】:

标签: python excel pandas python-3.7


【解决方案1】:

.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'

【讨论】:

    【解决方案2】:

    你可以使用 df[column_name][row_number]

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2015-04-30
    • 1970-01-01
    • 2022-11-02
    相关资源
    最近更新 更多