Search code examples
pythonpandasseries

Select pandas Series elements based on condition


Given a dataframe, I know I can select rows by condition using below syntax:

df[df['colname'] == 'Target Value']

But what about a Series? Series does not have a column (axis 1) name, right?

My scenario is I have created a Series by through the nunique() function:

sr = df.nunique()

And I want to list out the index names of those rows with value 1.

Having failed to find a clear answer on the Net, I resorted to below solution:

for (colname, coldata) in sr.iteritems(): 
   if coldata == 1: 
      print(colname) 

Question: what is a better way to get my answer (i.e list out index names of Series (or column names of the original Dataframe) which has just a single value?)

The ultimate objective was to find which columns in a DF has one and only one unique value. Since I did not know how to do that direct from a DF, I first used nunique() and that gave me a Series. Thus i needed to process the Series with a "== 1" (i.e one and only one)

I hope my question isnt silly.


Solution

  • It is unclear what you want. Whether you want to work on the dataframe or on the Series ?

    Case 1: Working on DataFrame

    In case you want to work on the dataframe to to list out the index names of those rows with value 1, you can try:

    df.index[df[df==1].any(axis=1)].tolist()
    

    Demo

    data = {'Col1': [0, 1, 2, 2, 0], 'Col2': [0, 2, 2, 1, 2], 'Col3': [0, 0, 0, 0, 1]}
    df = pd.DataFrame(data)
    
       Col1  Col2  Col3
    0     0     0     0
    1     1     2     0
    2     2     2     0
    3     2     1     0
    4     0     2     1
    

    Then, run the code, it gives:

    [1, 3, 4]
    

    Case 2: Working on Series

    If you want to extract the index of a Series with value 1, you can extract it into a list, as follows:

    sr.loc[sr == 1].index.tolist()
    

    or use:

    sr.index[sr == 1].tolist()