0

Assume I have a pandas datafra df and I have the columns A,B,C.

I want to take the index' idx=[1,10,12,17] from column B - how is that done?

I have tried df[idx,"B"], df.iloc[idx,"B"], df.loc[idx,"B"]

1
  • 1
    I think df.loc[idx,"B"] is nice solution Commented Dec 17, 2019 at 11:53

1 Answer 1

2

You can you .loc or .iloc.

idx = [1,10,12,17]
df = pd.DataFrame(np.random.rand(20, 3), columns=['A', 'B', 'C'])

df.loc[idx, 'B']
df.iloc[idx, 1]

Result:

1     0.532895
10    0.197801
12    0.978466
17    0.847575
Name: B, dtype: float64
Sign up to request clarification or add additional context in comments.

1 Comment

For some reason df.loc[idx,"B"] did not work before, but now it does. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.