Pandas get column contains character in all rows

somal :

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']
G. Anderson :

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']

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=386054&siteId=1