简体   繁体   English

Pandas 数据框按索引选择行,按名称选择列

[英]Pandas dataframe select row by index and column by name

Is there any way to select the row by index (ie integer) and column by column name in a pandas data frame?有没有办法在熊猫数据框中按索引(即整数)和按列名选择行?

I tried using loc but it returns an error, and I understand iloc only works with indexes.我尝试使用 loc 但它返回一个错误,我知道 iloc 仅适用于索引。

Here is the first rows of the data frame df.这是数据框 df 的第一行。 I am willing to select the first row, column named 'Volume' and tried using df.loc[0,'Volume']我愿意选择第一行,名为“Volume”的列并尝试使用 df.loc[0,'Volume']

在此处输入图片说明

Use get_loc method of Index to get integer location of a column name.使用 Index 的get_loc方法获取列名的整数位置。

Suppose this dataframe:假设这个数据框:

>>> df
    A  B  C
10  1  2  3
11  4  5  6
12  7  8  9

You can use .iloc like this:您可以像这样使用.iloc

>>> df.iloc[1, df.columns.get_loc('B')]
5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM