user5054
user5054

Reputation: 1096

Python: subsetting data frame using a list

I am a newbie to python and I have a very simple question. I have a list of indices which correspond to the some row indices of the data frame. What is the best way to use this list (in the order of the items) to subset a data frame?

Upvotes: 1

Views: 728

Answers (2)

gmds
gmds

Reputation: 19885

Use iloc:

import numpy as np
import pandas as pd

np.random.seed(0)
df = pd.DataFrame(np.random.randint(100, 200, (10, 2)), columns=['a', 'b'])
print(df, end='\n\n')
print(df.iloc[[7, 2, 3, 1, 6]])

Output:

    a    b
0  144  147
1  164  167
2  167  109
3  183  121
4  136  187
5  170  188
6  188  112
7  158  165
8  139  187
9  146  188

     a    b
7  158  165
2  167  109
3  183  121
1  164  167
6  188  112

If you want to use a list that corresponds to values in a column instead, then we need to merge:

values = [158, 167, 183, 164, 188]
print(pd.merge(pd.DataFrame([158, 167, 183, 164, 188], columns=['a']), df, on='a', how='left'))

Output:

     a    b
0  158  165
1  167  109
2  183  121
3  164  167
4  188  112

Upvotes: 1

Dirk van Eck
Dirk van Eck

Reputation: 1

"how can I do a similar subsetting where the list I have corresponds to the elements in the first column instead of the data frame indices?"

-->

[x for x in df['a'] if x in list_of_elements]

Upvotes: 0

Related Questions