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"]
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
df.loc[idx,"B"] did not work before, but now it does. Thanks :)
df.loc[idx,"B"]is nice solution