IQ Bot Custom Logic (Python): Efficient replacement process in a loop

Explaining efficiency improvement using loops related to Exclusion process and Replacement process I've done it, but I will explain how to make "replacement" more efficient in a loop with a very similar pattern.

What is "replacement"?

This is not a programming term. As introduced in this article, it is a process of "if a certain character string is included, replace it with another character string".

It's an application of replacement.

When do you use "replacement" in a loop?

For example, "I want to make the department name written in the abbreviation on the invoice statement the official name" or "I want to make the product name written in the abbreviation on the order form the official name". think.

How to do it! (Field item edition)

Replacement @ field item


sashikae_list = (("If this is included 1","Please replace it with this 1"),
               ("If this is included 2","Please replace it with this 2"),
               ("If this is included 3","Replace it with this 3"))

for i in sashikae_list:
  if i[0] in field_value:
    field_value = i[1]

The explanation is [this area] of the replacement edition (https://qiita.com/IQ_Bocchi/items/83ae1dedfc1de6962006#%E3%82%BA%E3%83%90%E3%83%AA%E3%82%84%E3 % 82% 8A% E3% 81% 8B% E3% 81% 9F% E7% BD% AE% E6% 8F% 9B% E3% 81% AE% E5% A0% B4% E5% 90% 88) please. It's almost the same mechanism.

How to do it! (Table edition)

Replacement @ table


sashikae_list = (("If this is included 1","Please replace it with this 1"),
               ("If this is included 2","Please replace it with this 2"),
               ("If this is included 3","Replace it with this 3"))

def table_sashikae(x,y): 
    for i in y:
        if i[0] in x:
            x = i[1]
    return x
        
df['Column name'] = df['Column name'].apply(table_sashikae,y=sasihkae_list)

that's all!

How was it?

This time, the mechanism is the same as Replacement processing and Replacement loop. Since it is only processed by the structure, the explanation is not so thick.

If you have any questions, please leave a comment on this article or contact us via DM on Twitter.

Recommended Posts

IQ Bot Custom Logic (Python): Efficient replacement process in a loop
IQ Bot Custom Logic (Python): Efficient replacement process in a loop
IQ Bot Custom Logic (Python): Streamline exclusions in loops
IQ Bot Custom Logic Basic Key
IQ Bot Custom Logic: Fixed Value Assignment
IQ Bot Custom Logic Related Processing Summary
Rewriting elements in a loop of lists (Python)
Use a custom error page in python / tornado
Create a loop antenna pattern in Python in KiCad
Daemonize a Python process
Custom sort in Python3
I implemented a Vim-like replacement command in Slackbot #Python
Loop through a generator that returns a date iterator in Python
IQ Bot Custom Logic: Correcting common reading habits on dates
Take a screenshot in Python
Create a function in Python
Create a dictionary in Python
Make a bookmarklet in Python
Draw a heart in Python
Easy! Implement a Twitter bot that runs on Heroku in Python
About psd-tools, a library that can process psd files in Python
IQ Bot Custom Logic: Delete the last n rows of the table
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
Create a DI Container in Python
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Handle requests in a separate process
Create a binary file in Python
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Draw a CNN diagram in Python
Create a random string in Python
Simple gacha logic written in Python
Schedule a Zoom meeting in Python
When writing a program in Python
Custom state space model in Python
Create a LINE Bot in Django
A story about a beginner making a VTuber notification bot from scratch in Python
I made a Discord bot in Python that translates when it reacts
Connect to postgreSQL from Python and use stored procedures in a loop.
Write a basic headless web scraping "bot" in Python with Beautiful Soup 4
IQ Bot Custom Logic: Split Application (Apply to Table, Include Error Control)
IQ Bot Custom Logic: 5 things you might be surprised when someone who speaks another language first touches Python