[英]Get a subset of data from one row of Dataframe
Let's say I have a dataframe df with columns 'A', 'B', 'C' Now I just want to extract row 2 of df and only columns 'B' and 'C'.假设我有一个包含“A”、“B”、“C”列的数据框 df 现在我只想提取 df 的第 2 行和“B”和“C”列。 What is the most efficient way to do that?
最有效的方法是什么?
Can you please tell me why df.ix[2, ['B', 'C']] didn't work?你能告诉我为什么 df.ix[2, ['B', 'C']] 不起作用吗?
Thank you!谢谢!
row_2 = df[['B', 'C']].iloc[1]
或者
# Convert column to 2xN vector, grab row 2 row_2 = list(df[['B', 'C']].apply(tuple, axis=1))[1]
Consider the dataframe df
考虑数据帧
df
df = pd.DataFrame(np.arange(9).reshape(3, 3), list('xyz'), list('ABC'))
df
A B C
x 0 1 2
y 3 4 5
z 6 7 8
If you want to maintain a dataframe如果你想维护一个数据框
df.loc[df.index[[1]], ['B', 'C']]
B C
y 4 5
If you want a series如果你想要一个系列
df.loc[df.index[1], ['B', 'C']]
B 4
C 5
Name: y, dtype: int64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.