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
Python Dictionary Comprehension
Changing the "Tick Frequency" on X or Y Axis in Matplotlib
Tkinter: How to Use After Method
Are For-Loops in Pandas Really Bad? When Should I Care
Using @Property Versus Getters and Setters
Sort a List by Multiple Attributes
Equivalent of Shell 'Cd' Command to Change the Working Directory
How to Resize an Image Using Pil and Maintain Its Aspect Ratio
Difference Between Del, Remove, and Pop on Lists
Django Template How to Look Up a Dictionary Value With a Variable
Recursive Function Returning None in Python
Retrieve Links from Web Page Using Python and Beautifulsoup
Saving Utf-8 Texts With Json.Dumps as Utf8, Not as \U Escape Sequence
Using Numpy to Build an Array of All Combinations of Two Arrays