When writing a Python script for a table item in IQ Bot, there is code that must be included at the beginning and end.
As I wrote in the article here, it is the following code.
How to write a Python script for a table item
#Code that must be entered when operating the table (first)
import pandas as pd
df = pd.DataFrame(table_values)
#In the middle part, put the code you want to process
#Code that must be entered when operating the table (last)
table_values = df.to_dict()
To a cute junior who uses IQ Bot in the company I was asked, "What is this code doing?" I tried my best to answer.
First of all, the programming language is called "library". There is something like a package that puts together instructions and functions to make it easier to process in a certain field.
Python has a library called pandas This pandas is a library that makes it easy to handle tables and arrays of data.
To use a library, you first need to import it.
The first line, ʻimport pandas as pd`, is the process for that.
The first line of the magic script
import pandas as pd
#Meaning: Import pandas.
#And make the contents of that pandas available under the name pd in the code I'm about to write.
The table information that IQ Bot read from the form was completely contained in a variable called table_values
.
(If you don't understand this story, see here)
The contents of the table_values
are difficult to handle as they are, so
Convert to a format called DataFrame suitable for handling with pandas.
Then, the converted data is stored in a variable named df
.
The second line of the magic script
df = pd.DataFrame(table_values)
#Meaning: table_Convert the contents of values to pandas DataFrame format and put it in a variable called df.
Where pd.
in pd.DataFrame
means pandas.
It is possible to abbreviate this as pd.
instead of writing pandas.
This is because I added ʻas pd to ʻimport pandas
in the first line of the magic code.
By processing the first and second lines, from here onward,
The entire contents contained in table_values are df
,
You will be able to handle the values of a specific column in it in the form df ['column name']
.
In the final process, the data that was previously stored in the variable df
in DataFrame format is
Revert to the format that the IQ Bot originally held the table_value
.
The original format of table_value
is dictionary type, so I will return to this format.
The end of the magic script
table_values = df.to_dict()
#Meaning: Convert the contents of df to dict type and table_Put it in values.
In this way, the processed result is handed over to the IQ Bot.
When importing Pandas, add ʻas pd, The name of the variable that stores the DataFrame is
df`.
It's not an absolute rule, but it's best to do this.
That's because the overwhelming majority of people write code that way.
Most of the commentary articles on Python and panda write code based on this naming premise.
Therefore, it is better to match the code when making IQ Bot. It makes it easier to find explanations when you stumble on something.
Even if you write it as follows, it will work if you just say whether the code works, I don't recommend it very much.
How to write a Python script for a table item
#Code that must be entered when operating the table (first)
import pandas as hoge1
hoge2 = hoge1.DataFrame(table_values)
#Code that must be entered when operating the table (last)
table_values = hoge2.to_dict()
--First, import a library called pandas --The table_values that IQ Bot has are put into a variable in DataFrame format that is easy to handle with pandas. --Finally, return the DataFrame format to the original format (dictionary type) of table_values and pass it to IQ Bot. ――Variable names work even if they are not magical, but it is convenient to keep them as magical.
Recommended Posts