我们如何获得一系列特定的过滤行?从熊猫数据框中获取特定的系列行
实施例数据帧:
>>> df = pd.DataFrame({'date': [20130101, 20130101, 20130102], 'location': ['a', 'a', 'c']})
>>> df
date location
0 20130101 a
1 20130101 a
2 20130102 c
我需要选择行,其中location
是c
为一系列。
我想:
row = df[df["location"] == "c"].head(1) # gives a dataframe
row = df.ix[df["location"] == "c"] # also gives a dataframe with single row
在这两种情况下,我不能行作为系列。
谢谢Boud这就像一个奇迹。 'df [df [“location”] ==“c”]。squeeze()'也可以正常工作。你能否也解释一下挤压? “挤压长度1维”意味着它将1行结果转换为系列? – Pratyush
@Pratyush我更新了上面的答案。我建议你调用squeeze,因为你想从1D数据框中获得一系列,这比仅仅为了投射目的调用iloc [0]更明确。 – Boud
不知道哪一个更好,但挤压pythonic :) +1解释 –