How to select a list of rows by name in a Pandas dataframe?

I am trying to extract strings from a Pandas frame using a list, but this is not possible. Here is an example

# df
    alleles  chrom  pos strand  assembly#  center  protLSID  assayLSID  
rs#
TP3      A/C      0    3      +        NaN     NaN       NaN        NaN
TP7      A/T      0    7      +        NaN     NaN       NaN        NaN
TP12     T/A      0   12      +        NaN     NaN       NaN        NaN
TP15     C/A      0   15      +        NaN     NaN       NaN        NaN
TP18     C/T      0   18      +        NaN     NaN       NaN        NaN

test = ['TP3','TP12','TP18']

df.select(test)

This is what I tried to do with just the list item and get this error TypeError: 'Index' object is not callable. What am I doing wrong?

+4
source share
2 answers

you can use df.loc[['TP3','TP12','TP18']]

Here is a small example:

In [26]: df = pd.DataFrame({"a": [1,2,3], "b": [3,4,5], "c": [5,6,7]})

In [27]: df.index = ["x", "y", "z"]

In [28]: df
Out[28]: 
   a  b  c
x  1  3  5
y  2  4  6
z  3  5  7

[3 rows x 3 columns]

In [29]: df.loc[["x", "y"]]
Out[29]: 
   a  b  c
x  1  3  5
y  2  4  6

[2 rows x 3 columns]
+4
source

You can select rows by position:

df.iloc[[0,2,4], :]
+1
source

Source: https://habr.com/ru/post/1619598/


All Articles