Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

Select all rows that contain the first x unique values in another column in Pandas [Python]

selected =df['col2'].unique().iloc[1:5]
apples = df[df['col2'].isin([selected])]
print(df)

Here is my pseudocode for what I'm trying to accomplish. I just want to get the first five unique values in column 2, and then subset the whole dataframe based on those values. I get this error on the first line:

AttributeError: 'numpy.ndarray' object has no attribute 'iloc'
question from:https://stackoverflow.com/questions/66061546/select-all-rows-that-contain-the-first-x-unique-values-in-another-column-in-pand

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The only issue is your array slicing

df = pd.DataFrame({"col2":np.random.randint(1,50,100)})
df[df["col2"].isin(df['col2'].unique()[:5])]

output

col2
0 3
1 13
2 1
3 27
4 4
9 1
20 13
27 1
31 4
35 4
42 13
43 27
48 3
59 4
60 4
67 4
90 3
95 4
96 4
98 13

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...