[英]Slice a pandas dataframe by index in a specific column
Assume I have a pandas datafra df
and I have the columns A,B,C
.假设我有一个 Pandas datafra
df
并且我有A,B,C
列。
I want to take the index' idx=[1,10,12,17]
from column B
- how is that done?我想从
B
列中获取索引' idx=[1,10,12,17]
- 这是怎么做的?
I have tried df[idx,"B"], df.iloc[idx,"B"], df.loc[idx,"B"]
我试过
df[idx,"B"], df.iloc[idx,"B"], df.loc[idx,"B"]
You can you .loc
or .iloc
.你可以
.loc
或.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.