从pandas dataframe获取特定行作为系列

时间:2021-12-23 18:51:53

How do we get a particular filtered row as series?

我们如何将特定的过滤行作为系列?

Example dataframe:

>>> df = pd.DataFrame({'date': [20130101, 20130101, 20130102], 'location': ['a', 'a', 'c']})
>>> df
       date location
0  20130101        a
1  20130101        a
2  20130102        c

I need to select the row where location is c as a series.

我需要选择位置为c的行作为一个系列。

I tried:

row = df[df["location"] == "c"].head(1)  # gives a dataframe
row = df.ix[df["location"] == "c"]       # also gives a dataframe with single row

In either cases I can't the row as series.

在任何一种情况下,我都不能将该行作为系列。

2 个解决方案

#1


47  

Use the squeeze function that will remove one dimension from the dataframe:

使用将从数据框中删除一个维度的squeeze函数:

df[df["location"] == "c"].squeeze()
Out[5]: 
date        20130102
location           c
Name: 2, dtype: object

DataFrame.squeeze method acts the same way of the squeeze argument of the read_csv function when set to True: if the resulting dataframe is a 1-len dataframe, i.e. it has only one dimension (a column or a row), then the object is squeezed down to the smaller dimension object.

当设置为True时,DataFrame.squeeze方法的作用与read_csv函数的squeeze参数相同:如果结果数据帧是1-len数据帧,即它只有一个维度(列或行),则对象是挤压到较小尺寸的物体。

In your case, you get a Series object from the DataFrame. The same logic applies if you squeeze a Panel down to a DataFrame.

在您的情况下,您从DataFrame获得一个Series对象。如果将Panel压缩到DataFrame,则适用相同的逻辑。

squeeze is explicit in your code and shows clearly your intent to "cast down" the object in hands because its dimension can be projected to a smaller one.

挤压在您的代码中是明确的,并清楚地显示您的意图“抛弃”手中的对象,因为它的尺寸可以投影到较小的尺寸。

If the dataframe has more than one column or row, squeeze has no effect.

如果数据框有多个列或行,则squeeze无效。

#2


12  

You can just take first row with integer indexing (iloc() function):

您可以使用整数索引(iloc()函数)获取第一行:

>>> df[df["location"] == "c"].iloc[0]
date        20130102
location           c
Name: 2, dtype: object

#1


47  

Use the squeeze function that will remove one dimension from the dataframe:

使用将从数据框中删除一个维度的squeeze函数:

df[df["location"] == "c"].squeeze()
Out[5]: 
date        20130102
location           c
Name: 2, dtype: object

DataFrame.squeeze method acts the same way of the squeeze argument of the read_csv function when set to True: if the resulting dataframe is a 1-len dataframe, i.e. it has only one dimension (a column or a row), then the object is squeezed down to the smaller dimension object.

当设置为True时,DataFrame.squeeze方法的作用与read_csv函数的squeeze参数相同:如果结果数据帧是1-len数据帧,即它只有一个维度(列或行),则对象是挤压到较小尺寸的物体。

In your case, you get a Series object from the DataFrame. The same logic applies if you squeeze a Panel down to a DataFrame.

在您的情况下,您从DataFrame获得一个Series对象。如果将Panel压缩到DataFrame,则适用相同的逻辑。

squeeze is explicit in your code and shows clearly your intent to "cast down" the object in hands because its dimension can be projected to a smaller one.

挤压在您的代码中是明确的,并清楚地显示您的意图“抛弃”手中的对象,因为它的尺寸可以投影到较小的尺寸。

If the dataframe has more than one column or row, squeeze has no effect.

如果数据框有多个列或行,则squeeze无效。

#2


12  

You can just take first row with integer indexing (iloc() function):

您可以使用整数索引(iloc()函数)获取第一行:

>>> df[df["location"] == "c"].iloc[0]
date        20130102
location           c
Name: 2, dtype: object