Answers for "pandas apply check for string length in column"

0

pandas apply check for string length in column

df=pd.read_csv('filex.csv')
df.A=df.A.apply(lambda x: x if len(x)== 10 else np.nan)
df.B=df.B.apply(lambda x: x if len(x)== 10 else np.nan)
df=df.dropna(subset=['A','B'], how='any')
Posted by: Guest on October-07-2021
0

pandas apply check for string length in column

#The *mask* variable is a dataframe of booleans, giving you True or False for the selected condition
mask = df[['A','B']].applymap(lambda x: len(str(x)) == 10)

#Here you can just use the mask to filter your rows, using the method *.all()* to filter only rows that are all True, but you could also use the *.any()* method for other needs
df = df[mask.all(axis=1)]
Posted by: Guest on October-07-2021

Code answers related to "pandas apply check for string length in column"

Python Answers by Framework

Browse Popular Code Answers by Language