Get particular row as series from pandas dataframe

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.


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

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

Tags:

Python

Pandas