Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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')
Comment

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)]
Comment

PREVIOUS NEXT
Code Example
Python :: how to check if a variable in python is a specific data type 
Python :: how to separate url from text in python 
Python :: python remove duplicates 
Python :: create a 2d array in python 
Python :: deleting a file using python 
Python :: get channle from id discord.py 
Python :: django template add numbers 
Python :: save to xlsx in python 
Python :: start virtual environment python linux 
Python :: pandas description of dataframe renaming column values 
Python :: creating an apis with python and flask 
Python :: list to csv python 
Python :: odoo order by xml rpc 
Python :: py2exe no console 
Python :: celery timezone setting django 
Python :: python for android 
Python :: hash python png 
Python :: python time limit for input 
Python :: convert pandas dataframe to numpy dataframe 
Python :: check status code urllib open 
Python :: run a python script from another python script on a raspberry pi 
Python :: string in list py 
Python :: list reverse method in python 
Python :: python prime number sum 
Python :: python enum to int 
Python :: use mongo replica set python 
Python :: convert integer to binary in python 
Python :: finding random numbers python 
Python :: python scipy moving average 
Python :: WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. buildozer 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =