#I tried something like Vlookup with Python # 2

---Purpose--- Read daily changes such as inventory information from the database and pour it into the specified data. Export in the specified format.

Interpreter used: Python3.8

--- Poster's work environment ---    Windows10Pro 64Bit

code

#!/usr/bin/env python
# -*- coding: utf-8 -*-

###Module used###
import sqlite3
import pandas as pd
import datetime

######Get timestamp
ThisDate = datetime.datetime.today().strftime("%Y%m%d")

#CurStocks <<Variable for the data frame that contains the database of the referencing inventory master


######Convert inventory information master to inventory reference data frame
#Here, the data of the inventory reference source is pulled from the file of SQlite3 format,
#Convert your favorite format to dataframe with Pandas.

dbpath = 'C:/Reference source/Inventory SQLite/Zaiko.db' 
conn = sqlite3.connect(dbpath)
cursor = conn.cursor()
cursor=cursor.execute('SELECT * FROM TurnOver_Data') 
CurStocks = pd.DataFrame(cursor.fetchall())
CurStocks.columns=["index", "Product name", "Product CD", "barcode", "stock"]
conn.close()
#(Remarks) ↑ The above method "cursor".fetchall()"
#The title of the column will be omitted, so I will add it later.
#The reference source data is based on the above column title.


######Prepare a Sqlite format file for pouring

print("● Update inventory contact file")
dbpath = 'C:/Pouring destination/Customer_Inventory FMT.db'
conn = sqlite3.connect(dbpath)
df_files=("C:/Pour destination base file/Customer_Inventory FMT.xlsx")
#I make a separate list of pouring destinations in an Excel file, but this is when the number of target items increases, etc.
#Because of the method of creating a base list with an Excel file and rewriting it for easy maintenance

df = pd.read_excel(df_files,encoding="cp932")
Chlist = (df["JAN"].transpose())
#df["JAN"].transpose()<<The data of "JAN" is converted so that it can be read one by one.
#The column of the pouring destination is given the title "JAN" which is the read key.


######The data for inventory reference is turned and referenced sequentially.

for item in Chlist:
    CurStocksult = CurStocks.query('barcode== @item')
    #Here, we will refer to the JAN code that is the key of the data frame of the reference source created earlier and turn it.
    #Reference source column: "Barcode" Pouring destination: "JAN" column title
    #Here, the data frame of the reference source is read one by one.

    if len(CurStocksult) >= 1:
        #Inventory count: If the reference frame has the JAN, refer to the data position.
        CurStocksult = (CurStocksult.iat[0, 3])

        #Get index number (data position of list of secondary array)
        DFindex=int(df.query('JAN == @item').index.astype("int64").values[0])

        if(CurStocksult) < 3 :
            Buf = 0
            df.iat[DFindex, 7] = "Shortage"
        else:
            Buf=CurStocksult
            df.iat[DFindex, 7] = ""
            
        df.iat[DFindex, 6]=Buf
        df.iat[DFindex, 8]=ThisDate
        #The 6th column of the pouring destination is the inventory column, the 7th column is the flag column, and the 8th column is the updated time stamp.

df.to_sql("Stock_Data", conn, if_exists="replace")
df.to_csv('C:/Updated inventory data.csv')
#Updated Sqlite and CSV.

conn.close()
print("● Update completed")

del df
del df_files

Clogged points, etc.

1: Create a list for loop processing Chlist = (df["JAN"].transpose()) In order to process the list of pouring destinations one by one, the key list of pouring destinations I converted it so that it can be turned in a loop

2: Acquisition of index number DFindex=int(df.query('JAN == @item').index.astype("int64").values[0]) Conversion process to get the data position (Y-axis?) Of the list of quadratic arrays

Recommended Posts

#I tried something like Vlookup with Python # 2
I tried fp-growth with python
I tried scraping with Python
I tried gRPC with Python
I tried web scraping with python.
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried sending an email with python.
I tried non-photorealistic rendering with Python + opencv
I tried a functional language with Python
I tried Python> autopep8
I tried Python> decorator
I tried "smoothing" the image with Python + OpenCV
I tried hundreds of millions of SQLite with python
I tried "differentiating" the image with Python + OpenCV
I tried L-Chika with Raspberry Pi 4 (Python edition)
I tried Jacobian and partial differential with python
I tried to get CloudWatch data with Python
I tried using mecab with python2.7, ruby2.3, php7
I tried function synthesis and curry with python
I tried "binarizing" the image with Python + OpenCV
I tried running faiss with python, Go, Rust
I tried running Deep Floor Plan with Python 3.6.10.
I tried sending an email with SendGrid + Python
I tried Learning-to-Rank with Elasticsearch!
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I made blackjack with python!
I tried to touch the CSV file with Python
I tried to draw a route map with Python
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried clustering with PyCaret
I tried to solve the soma cube with python
I tried to get started with blender python script_Part 02
I tried to implement an artificial perceptron with python
Forcibly draw something like a flowchart with Python, matplotlib
I tried to automatically generate a password with Python3
I tried Python C extension
[Python] I tried using OpenPose
Mayungo's Python Learning Episode 1: I tried printing with print
I tried to analyze J League data with Python
I made blackjack with Python.
I tried hitting the API with echonest's python client
I tried to solve AOJ's number theory with Python
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I tried deploying Kubernetes Pods / Helm Chart with Pulumi (Python)
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
I tried using the Python library from Ruby with PyCall
I tried to make various "dummy data" with Python faker
I tried various methods to send Japanese mail with Python
I tried sending an email from Amazon SES with Python
I want to do something like sort uniq in Python
I tried running Movidius NCS with python of Raspberry Pi3
I tried follow management with Twitter API and Python (easy)
Mayungo's Python Learning Episode 3: I tried to print numbers with print
I tried a stochastic simulation of a bingo game with Python
I tried to divide the file into folders with Python
I tried to touch Python (installation)
I tried trimming efficiently with OpenCV
I can't install python3 with pyenv-vertualenv