Reputation: 565
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'
Upvotes: 0
Views: 552
Reputation: 31206
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])]
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 |
Upvotes: 1