The most frequently used process in IQ Bot's custom logic is "replacement". Since "exclusion" of unnecessary characters and symbols is also a kind of "replacement", it is no exaggeration to say that 80 to 90% of the cases where custom logic is used.
The basic method of replacement / exclusion is introduced in this article, but today, in such replacement, the processing of "exclusion" is I will show you how to use loops to improve efficiency.
Let's assume that extra symbols are mixed in the acquired items due to faint printing.
If "Dokodemo Shoji" becomes "Dokodemo Shoji." (With a dot at the end), exclude it like this.
Normal replacement (exclusion)
field_value = field_value.replace(".","")
The above is good because there was only one type of noise (dot), but what should I do if many types of noise are mixed, such as "Do * ko; de', mo: commerce! Things." ??
If you do it properly, it will be as follows.
Normal replacement (exclusion)
field_value = field_value.replace("*","")
field_value = field_value.replace(";","")
field_value = field_value.replace("'","")
field_value = field_value.replace(",","")
field_value = field_value.replace(":","")
field_value = field_value.replace("!","")
field_value = field_value.replace(".","")
If you want to exclude other noises such as "#"! If you think so, add the logic field_value = field_value.replace ("#", "")
.
This is not a mistake, but the story is that you can write with a little less logic.
You can do exactly the same thing as above with the code below.
Use loops to streamline exclusions
ignore_list = ("*",";","'",",",":","!",".")
for i in ignore_list:
field_value = field_value.replace(i,"")
If you want to exclude the noise of "#" in the above! If you think so, just add the # element to the end of ʻignore_list`.
ʻIgnore_list = ("*", ";", "'", ",", ":", "!", ".", "#") . The element with the last
" # "` added.
The basic grammar when Python loops over a sequence is [here](https://qiita.com/imuimu/items/1e1581bd0749a871c213#%E3%83%AA%E3%82%B9%E3%83] % 88% E9% 85% 8D% E5% 88% 97% E3% 81% AE% E3% 83% AB% E3% 83% BC% E3% 83% 97% E5% 87% A6% E7% 90% 86 )is.
What is a sequence? If you think, please refer to this article (external link).
For more information for beginners, click here [https://qiita.com/IQ_Bocchi/items/4e9fb40f9a69b22c301a#%E3%83%AA%E3%82%B9%E3%83%88%E3%81%A3%E3 % 81% A6% E4% BD% 95). The link destination explains "list" which is a kind of sequence, but please think that there are various kinds of lockers other than "list".
By the way, ʻignore_list` in the above code is a sequence of type called tuple.
So, the following is a loop that processes the elements inside the tuple one by one. The following is an explanation of what each line is doing.
Use loops to streamline exclusions
for i in ignore_list: #ignore_Put each element of list in a variable called i and process them in order.
field_value = field_value.replace(i,"") #field_Excluding i in value, field_Substitute for value
Efficiency with loops can also be applied to tables.
In the case of a table, you can change the contents of the for statement to the grammar for the table and process it as follows ...
Use loops to streamline exclusions (for tables)
ignore_list = ("Characters you want to exclude 1","Characters you want to exclude 2","Characters you want to exclude 3")
for i in ignore_list:
df['Column name'] = df['Column name'].str.replace(i,"")
I think the following are beautiful.
Use loops to streamline exclusions (for tables)
ignore_list = ("Characters you want to exclude 1","Characters you want to exclude 2","Characters you want to exclude 3")
def table_ignore(x,y):
for i in y:
x = x.y(i,"")
return x
df['Column name'] = df['Column name'].apply(table_replace,y=ignore_list)
At first glance, the amount of code seems to be larger at the bottom, but if you can have multiple sets of strings you want to exclude, or if you want to freely apply those combinations to multiple columns, the bottom is more flexible. ..
How was it? My motto is always for beginners, but I think today's article was for programming beginners.
If you have any questions, please leave a comment on this article or contact us via DM on Twitter.
Recommended Posts