DevOps Engineer. Solutions Architect

Blog 10 Spring 2021: Expanding Upon Python CSV Data Formatting

Information Systems and Data Analysts are always facing thousands upon thousands of data entries everyday with the given task of framing each entry. Sometimes individuals from management request data in the form of CSVs as it allows for simple access on software such as Google Spreadsheets or Microsoft Excel. A simple CSV file is a friendlier way of presenting data rather than traditional forms like JSON as it allows for corporate to easily read.

This will be a quick guide on creating a script that utilizes Python CSV and Pandas data frame modules.

Using Python CSV

  1. Create a new executable python script and import the csv Python package.
    #!/usr/local/bin/python3
    import sys
    import os
    import csv
    
  2. In the Python function make sure to call in the list of data entries that will be passed.
    list_of_entries = ["red", "blue", 2, "shoes", 4.4]
    
  3. Proceed to writing a function that formats the data into CSV. Feel free to follow module documentation on this page.
    with open('eggs.csv', 'w', newline='') as csvfile:
     spamwriter = csv.writer(csvfile, delimiter=' ',
                             quoting=csv.QUOTE_NONNUMERIC)
     spamwriter.writerow(list_of_entries)
    
  4. Desired output should meet any listed parameters.
    terminal$: ./script.py
    "red","blue",2,"shoes,4.4
    

Using Python Pandas

  1. Alternative choice is to use the Python Pandas module. Proceed by importing the module.
import pandas as pd

df = pd.read_csv('sample.csv', index_col=0)
print(df)
  1. Continue by writing a script that either reads a pre-existing file or list. ``` cities = pd.DataFrame([[‘Sacramento’, ‘California’], [‘Miami’, ‘Florida’]], columns=[‘City’, ‘State’]) cities.to_csv(‘cities.csv’)

df = pd.read_csv(‘cities.csv’) print(df)


3. Example output 

Unnamed: 0 City State 0 0 Sacramento California 1 1 Miami Florida ```