从pandas数据框中获取特定行作为系列 [英] Get particular row as series from pandas dataframe

查看:175
本文介绍了从pandas数据框中获取特定行作为系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取特定的已过滤行作为序列?

示例数据框:

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

我需要选择locationc 作为系列的行.

我尝试过:

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

在任何一种情况下,我都无法将该行作为系列.

解决方案

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

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

设置为True时,

DataFrame.squeeze方法的作用与read_csv函数的squeeze自变量相同:如果结果数据帧为1 len数据帧,即,它仅具有一个维度(一列或一行),然后将对象压缩到较小尺寸的对象.

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

挤压在您的代码中是明确的,并且可以清楚地表明您打算压铸"手中的物体,因为它的尺寸可以投影到较小的物体.

如果数据框具有多于一列或一行,则挤压无效.

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.

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.

解决方案

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

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.

In your case, you get a Series object from the DataFrame. The same logic applies if you squeeze a Panel down to a 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.

这篇关于从pandas数据框中获取特定行作为系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆