Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas How to filter a Series

Tags:

python

pandas

I have a Series like this after doing groupby('name') and used mean() function on other column

name
383      3.000000
663      1.000000
726      1.000000
737      9.000000
833      8.166667

Could anyone please show me how to filter out the rows with 1.000000 mean values? Thank you and I greatly appreciate your help.

like image 895
Kiem Nguyen Avatar asked Oct 06 '22 08:10

Kiem Nguyen


People also ask

How do you sort a panda series?

Sort the Series in Ascending Order By default, the pandas series sort_values() function sorts the series in ascending order. You can also use ascending=True param to explicitly specify to sort in ascending order. Also, if you have any NaN values in the Series, it sort by placing all NaN values at the end.

Can you slice a Pandas series?

Pandas str. slice() method is used to slice substrings from a string present in Pandas series object. It is very similar to Python's basic principal of slicing objects that works on [start:stop:step] which means it requires three parameters, where to start, where to end and how much elements to skip.

What does the pandas series filter() method do?

What does the pandas series.filter () method do? The series.filter () method in the pandas series constructor is used to subset the rows of a series object based on the index labels. The filter method does not work for the content of the series object, it is only applied to the index labels of the series object.

How do I filter pandas Dataframe?

Pandas Series.filter () function returns subset rows or columns of dataframe according to labels in the specified index. Please note that this routine does not filter a dataframe on its contents. The filter is applied to the labels of the index.

How to filter out some values in pandas series using regular expressions?

Example #1: Use Series.filter () function to filter out some values in the given series object using a regular expressions. import pandas as pd sr = pd.Series ([80, 25, 3, 25, 24, 6]) index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']

How to subset the rows of a series object in pandas?

The series.filter () method in the pandas series constructor is used to subset the rows of a series object based on the index labels. The filter method does not work for the content of the series object, it is only applied to the index labels of the series object.


2 Answers

In [5]:

import pandas as pd

test = {
383:    3.000000,
663:    1.000000,
726:    1.000000,
737:    9.000000,
833:    8.166667
}

s = pd.Series(test)
s = s[s != 1]
s
Out[0]:
383    3.000000
737    9.000000
833    8.166667
dtype: float64
like image 197
Andrew Avatar answered Oct 28 '22 07:10

Andrew


From pandas version 0.18+ filtering a series can also be done as below

test = {
383:    3.000000,
663:    1.000000,
726:    1.000000,
737:    9.000000,
833:    8.166667
}

pd.Series(test).where(lambda x : x!=1).dropna()

Checkout: http://pandas.pydata.org/pandas-docs/version/0.18.1/whatsnew.html#method-chaininng-improvements

like image 37
DACW Avatar answered Oct 28 '22 07:10

DACW