Use a List of Values to Select Rows from a Pandas Dataframe

Use a list of values to select rows from a Pandas dataframe

You can use the isin method:

In [1]: df = pd.DataFrame({'A': [5,6,3,4], 'B': [1,2,3,5]})

In [2]: df
Out[2]:
A B
0 5 1
1 6 2
2 3 3
3 4 5

In [3]: df[df['A'].isin([3, 6])]
Out[3]:
A B
1 6 2
2 3 3

And to get the opposite use ~:

In [4]: df[~df['A'].isin([3, 6])]
Out[4]:
A B
0 5 1
3 4 5

Use a list of same values to select rows from a dataframe

Set Id as the index, use .loc, and reset Id back to a column:

df.set_index('Id').loc[[1, 1]].reset_index()

# Id Age
# 0 1 20
# 1 1 20

Filter dataframe rows if value in column is in a set list of values

Use the isin method:

rpt[rpt['STK_ID'].isin(stk_list)]

How to select a part of a dataframe according to a list?

Use df.loc:

df.loc[df['Name'].isin(list_americ)]

Randomly selecting a subset of rows from a pandas dataframe based on existing column values

I hope this code snippet will work for you

samples = []
for group in df.GroupID.unique():
s = df.loc[df.GroupID== group].sample(n=1).reset_index(drop=True)
samples.append(s)

sample = pd.concat(samples, axis=0)

The code will take each 'GroupID' and sample observation from that subgroup.
You can concatenate the subsamples (with one GroupID) for the needed sample.



Related Topics



Leave a reply



Submit