목록Note (470)
Note
How to filter valid emails from a series? # Input emails = pd.Series(['buying books at amazom.com', 'rameses@egypt.com', 'matt@t.co', 'narendra@modi.com']) # 1 (as series of strings) import re pattern ='[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}' mask = emails.map(lambda x: bool(re.match(pattern, x))) emails[mask] # 2 (as series of list) emails.str.findall(pattern, flags=re.IGNORECASE) # 3..
How to print the full numpy array without truncating # Input np.set_printoptions(threshold=6) a = np.arange(15) # Solution np.set_printoptions(threshold=np.nan) a # output array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
How to filter words that contain atleast 2 vowels from a series? # Input ser = pd.Series(['Apple', 'Orange', 'Plan', 'Python', 'Money']) # Solution from collections import Counter mask = ser.map(lambda x: sum([Counter(x.lower()).get(i, 0) for i in list('aeiou')]) >= 2) ser[mask] # output 0 Apple 1 Orange 4 Money dtype: object
How to limit the number of items printed in output of numpy array? np.set_printoptions(threshold=6) a = np.arange(15) a # output array([ 0, 1, 2, ..., 12, 13, 14])
How to convert year-month string to dates corresponding to the 4th day of the month? import pandas as pd # Input ser = pd.Series(['Jan 2010', 'Feb 2011', 'Mar 2012']) # 1 from dateutil.parser import parse # Parse the date ser_ts = ser.map(lambda x: parse(x)) # Construct date string with date as 4 ser_datestr = ser_ts.dt.year.astype('str') + '-' + ser_ts.dt.month.astype('str') + '-' + '04' # Form..