Python: Pandas filter string data based on its string length Python: Pandas filter string data based on its string length python python

Python: Pandas filter string data based on its string length


import pandas as pddf = pd.read_csv('filex.csv')df['A'] = df['A'].astype('str')df['B'] = df['B'].astype('str')mask = (df['A'].str.len() == 10) & (df['B'].str.len() == 10)df = df.loc[mask]print(df)

Applied to filex.csv:

A,B123,abc1234,abcd1234567890,abcdefghij

the code above prints

            A           B2  1234567890  abcdefghij


A more Pythonic way of filtering out rows based on given conditions of other columns and their values:

Assuming a df of:

data={"names":["Alice","Zac","Anna","O"],"cars":["Civic","BMW","Mitsubishi","Benz"],     "age":["1","4","2","0"]}df=pd.DataFrame(data)df:  age        cars  names0   1       Civic  Alice1   4         BMW    Zac2   2  Mitsubishi   Anna3   0        Benz      O

Then:

df[df['names'].apply(lambda x: len(x)>1) &df['cars'].apply(lambda x: "i" in x) &df['age'].apply(lambda x: int(x)<2)  ]

We will have :

  age   cars  names0   1  Civic  Alice

In the conditions above we are looking first at the length of strings, then we check whether a letter ("i") exists in the strings or not, finally, we check for the value of integers in the first column.


I personally found this way to be the easiest:

df['column_name'] = df[df['column_name'].str.len()!=10]