This article mainly introduces the python data cleansing string processing information. For more information, see this article, for more information, see
Preface
Data cleansing is a complex and tedious task. it is also the most important part in the entire data analysis process. Some people say that 80% of the time spent on an analysis project is cleaning data, which sounds incredible, but it is true in actual work. There are two purposes for data cleansing: cleaning to make data available. The second is to make the data more suitable for subsequent analysis. In other words, there are dirty data to be washed, and clean data to be washed.
In data analysis, especially text analysis, character processing requires a lot of effort. Therefore, understanding character processing is also an important capability for data analysis.
String processing method
First, let's first understand what basic methods are available.
First, let's take a look at the split method of the string.
str='i like apple,i like bananer'print(str.split(','))
Result of splitting str with commas:
['I like apple',' I like bananer ']
print(str.split(' '))
Result of split by space:
['I', 'like', 'Apple, I ', 'like', 'bananer']
print(str.index(','))print(str.find(','))
Both search results are:
12
If the index cannot be found, an error is returned. find returns-1.
print(str.count('i'))
Result:
4
Connt is used to count the frequency of the target string.
print(str.replace(',', ' ').split(' '))
Result:
['I', 'like', 'apple', 'I', 'like', 'bananer']
Here, replace the comma with a space, and then use a space to separate the string, so that each word can be extracted.
In addition to the conventional method, the more powerful character processing tool fee regular expression does not belong.
Regular expression
Before using a regular expression, we must first understand the many methods in the regular expression.
Next, let's take a look at the usage of the next method. First, let's take a look at the differences between the match and search methods.
str = "Cats are smarter than dogs"pattern=re.compile(r'(.*) are (.*?) .*')result=re.match(pattern,str)for i in range(len(result.groups())+1): print(result.group(i))
Result:
Cats are smarter than dogs
Cats
Smarter
In this form of pettern matching rules, the results returned by the match and search methods are the same.
If you change pattern
pattern=re.compile(r'are (.*?) .*')
Match returns none, and search returns the following results:
Are smarter than dogs
Smarter
Next, let's take a look at the usage of other methods.
str = "138-9592-5592 # number"pattern=re.compile(r'#.*$')number=re.sub(pattern,'',str)print(number)
Result:
138-9592-5592
The above is to extract the number by replacing the content after # with null.
We can further replace the horizontal bar of the number.
print(re.sub(r'-*','',number))
Result:
13895925592
We can also use the find method to print the found string.
str = "138-9592-5592 # number"pattern=re.compile(r'5')print(pattern.findall(str))
Result:
['5', '5', '5']
Regular expressions have a lot of content, so we need to have a good understanding of the rules for matching strings. below are specific matching rules.
Vectorized string functions
When clearing the scattered data to be analyzed, you often need to normalize strings for chemical engineering.
data = pd.Series({'li': '120@qq.com','wang':'5632@qq.com', 'chen': '8622@xinlang.com','zhao':np.nan,'sun':'5243@gmail.com'})print(data)
Result:
You can also split the string to extract the required string.
Data = pd. series ({'Lil': '2014 @ qq.com ', 'Wang': '2014 @ qq.com', 'Chen': '2014 @ xinlang.com ', 'zhao': np. nan, 'Sun': '2014 @ gmail.com '}) pattern = re. compile (r' (\ d *) @ ([a-z] + )\. ([a-z] {2, 4}) ') result = data. str. match (pattern) # Here the fillall method can also be used as result = data. str. findall (pattern) print (result)
Result:
Chen [(8622, xinlang, com)]
Li [(120, qq, com)]
Sun [(5243, gmail, com)]
Wang [(5632, qq, com)]
Zhao NaN
Dtype: object
In this case, we need to extract the name before the mailbox.
print(result.str.get(0))
Result:
Of course, you can also use slices for extraction, but the extracted data is not accurate.
data = pd.Series({'li': '120@qq.com','wang':'5632@qq.com', 'chen': '8622@xinlang.com','zhao':np.nan,'sun':'5243@gmail.com'})print(data.str[:6])
Result:
Summary