Join csv normalized by Python pandas to make it easier to check

Processed to make it easier to check by joining the normalized csv

Overview

RDBMS is basically normalized and stores data.

For example In a transaction Date, product code, sales quantity To the master Product code, product name, product classification etc And so on.

If it is stored in RDBMS and you have an environment to access it You can check the contents by combining them in one shot with the SQL SELECT statement.

However, I only have the CSV for each table For some reason, it was necessary to check or confirm the contents. Is there a situation? I wonder if there is not much w

For that reason, assuming that you have a normalized CSV (that is, a CSV output of the RDBMS table as it is for each table) Here is a sample that combines them to generate easy-to-read Excel.

Usage environment

Python 3.8.1 Visual Studio Code 1.41.1

Test data preparation

Python3でcsv処理あれこれテスト As with the test data, CSV of the list of listed companies Obtain and use the one distributed by Mr. Kabusapo. https://kabusapo.com/stock-data/stock-list/

I dare to normalize this (although not so much) Stock code, number of shares constituting one unit Brand code, brand name Divide into two CSVs.

csv_join_pre.py



#csv_join data preparation

import pandas as pd

def main():
    #Body processing
    stoc_value_table = pd.read_csv(".\\CSV\\stocklist.csv",encoding="utf-8", dtype=str )

    #CSV, split output
    stoc_value_table.to_csv(".\\CSV\\tangen.csv",encoding="utf-8",columns=['Stock code', 'Number of shares constituting one unit'],header=True, index=False)
    stoc_value_table.to_csv(".\\CSV\\name_master.csv",encoding="utf-8",columns=['Stock code', 'trading name'],header=True, index=False)
    

if __name__ == '__main__': main()

Source code

As shown below, join and finally output Excel for confirmation

pandas_csv_join_sample.py



#Processed to make it easier to check by joining the normalized csv

import pandas as pd


def main():
    #Body processing
    #open csv data(Master and transaction)
    brand_master = pd.read_csv(".\\CSV\\name_master.csv",encoding="utf-8", dtype=str)
    tangen_tran = pd.read_csv(".\\CSV\\tangen.csv",encoding="utf-8", dtype=str)

    #Left join with the brand code.
    join_brand = pd.merge(tangen_tran, brand_master, how="left", on="Stock code")
    
    #2 Sample to join another table with one key
    #vew_brand = pd.merge(join_brand, cat_master, how="left", left_on=['A','B'], right_on=['C','D'])

    #Excel output
    join_brand.to_excel(".\\CSV\\vew_brand.xlsx",columns=['Stock code', 'trading name', 'Number of shares constituting one unit'])
    

if __name__ == '__main__': main()

Recommended Posts

Join csv normalized by Python pandas to make it easier to check
Tips to make Python here-documents easier to read
[Python] Continued-Convert PDF text to CSV page by page
Expand devicetree source include to make it easier to read
[Python] Convert PDF text to CSV page by page (2/24 postscript)
How to convert JSON file to CSV file with Python Pandas
Make a CSV formatting tool with Python Pandas PyInstaller
[Python] A memo to write CSV vertically with Pandas
How to save a table scraped by python to csv
Precautions when inputting from CSV with Python and outputting to json to make it an exe
Try to make it using GUI and PyQt in Python
[Python] Open the csv file in the folder specified by pandas
[Python] How to make a list of character strings character by character
Color Python errors (stack traces) to make them easier to understand
Make the display of Python module exceptions easier to understand
You who color the log to make it easier to see
One liner that formats JSON to make it easier to see
Read csv with python pandas
[Python] Convert list to Pandas [Pandas]
Write to csv with Python
I tried to make it possible to automatically send an email just by double-clicking the [Python] icon
I wrote Django commands to make it easier to debug Celery tasks
Code Python to check and graph if it follows Benford's law
Python hand play (RDKit descriptor calculation: SDF to CSV using Pandas)
[Good By Excel] python script to generate sql to convert csv to table
Make it easier to test programs that work with APIs with vcrpy
I tried to make it possible to automatically send an email just by double-clicking the [GAS / Python] icon
[Python] Loading csv files using pandas
Make apache log csv with python
[Python] Write to csv file with Python
[Python] How to use Pandas Series
Output to csv file with Python
[Python] Join two tables with pandas
Make Python dict accessible by Attribute
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Python] How to read a csv file (read_csv method of pandas module)
Read CSV file with Python and convert it to DataFrame as it is
Make it easy to check AGL release software using SW360 / SPDX Lite
[Python] Try to make a sort program by yourself. (Selection sort, insertion sort, bubble sort)
[Zaif] I tried to make it easy to trade virtual currencies with Python