Pandas get column contains character in all rows

Viewed 89

I want to get list of dataframe columns that contains all rows with 2 spaces.

Input:

import pandas as pd
import numpy as np
pd.options.display.max_columns = None
pd.options.display.max_rows = None
pd.options.display.expand_frame_repr = False

df = pd.DataFrame({'id': [101, 102, 103],
                   'full_name': ['John Brown', 'Bob Smith', 'Michael Smith'],
                   'comment_1': ['one two', 'qw er ty', 'one space'],
                   'comment_2': ['ab xfd xsxws', 'dsd sdd dwde', 'wdwd ofjpoej oihoe'],
                   'comment_3': ['ckdf cenfw cd', 'cewfwf wefep lwcpem', np.nan],
                   'birth_year': [1960, 1970, 1970]})

print(df)

Output:

    id      full_name  comment_1           comment_2            comment_3  birth_year
0  101     John Brown    one two        ab xfd xsxws        ckdf cenfw cd        1960
1  102      Bob Smith   qw er ty        dsd sdd dwde  cewfwf wefep lwcpem        1970
2  103  Michael Smith  one space  wdwd ofjpoej oihoe                  NaN        1970

Expected Output:

['comment_2', 'comment_3']
2 Answers

You can use series.str.count() to count the appearances of a substring or pattern in a string, use .all() to check whether all items meet the criteria, and iterate over df.columns using only string columns with select_dtypes('object')

[i for i in df.select_dtypes('object').columns if (df[i].dropna().str.count(' ')==2).all()]    
['comment_2', 'comment_3']

Try:

res=[]
for col in df.columns:
    if(df[col].dtype==object):
        dftemp=df[col].fillna("  ").str.replace(r"[^\s]", "").str.len()
        dftemp=dftemp.eq(2).all()
        if(dftemp): res.append(col)

print(res)

Outputs:

['comment_2', 'comment_3']

It runs through all columns, which might be strings (object type), removes all the non-space characters from these columns, then just counts charcters. In case if all have exactly 2 characters - it adds column name to the res array.

Related