The following should work for you. Here I sample remove_n
random row_ids from df
's index. After that df.drop
removes those rows from the data frame and returns the new subset of the old data frame.
import pandas as pd
import numpy as np
np.random.seed(10)
remove_n = 1
df = pd.DataFrame({"a":[1,2,3,4], "b":[5,6,7,8]})
drop_indices = np.random.choice(df.index, remove_n, replace=False)
df_subset = df.drop(drop_indices)
DataFrame df
:
a b
0 1 5
1 2 6
2 3 7
3 4 8
DataFrame df_subset
:
a b
0 1 5
1 2 6
3 4 8
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…