Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get particular row as series from pandas dataframe

Tags:

python

pandas

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.

like image 853
Pratyush Avatar asked Oct 25 '13 21:10

Pratyush


People also ask

How do you access a row in a DataFrame in Python?

You can use the loc and iloc functions to access rows in a Pandas DataFrame.

Is each row in a DataFrame a series?

Each column in a DataFrame is a Series A pandas Series has no column labels, as it is just a single column of a DataFrame . A Series does have row labels.

How do I read a specific row in Excel using pandas?

To tell pandas to start reading an Excel sheet from a specific row, use the argument header = 0-indexed row where to start reading. By default, header=0, and the first such row is used to give the names of the data frame columns. To skip rows at the end of a sheet, use skipfooter = number of rows to skip.


1 Answers

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.

like image 74
Zeugma Avatar answered Oct 25 '22 14:10

Zeugma