Love for Pandas

By Prajwal Haniya

Techletter #99 | Novemeber 16, 2024

I have been programming for a good amount of time now! Recently I used pandas. I fell in love with it. And can guarantee you that pandas have a greater amount of capability to manipulate the data than any other libraries.

Python is the go-to language when it comes to data processing and getting inferences from it.

Recently, I coded a simple script that takes the address string extracts the picode, and maps the city with it. Solving the same problem with other programming languages is also easy, but the way the rows and columns can be manipulated with Python is mind-boggling, and haven’t experienced it in JavaScript. It took me just around 2 hours to code it, testing on more than a lakh data rows, and deploy it to production. Probably with JS, I would have taken a little more time as there are no libraries like pandas, even if they exist, the community support for pandas is huge.

Let us code a simple tool that converts the JSON data into Excel using pandas.

import pandas as pd

def json_to_excel(json_data, output_file):
    try:
        df = pd.DataFrame(json_data)

        df.to_excel(output_file, index=False)
        print(f"Excel file saved successfully at {output_file}")
    except Exception as e:
        print(f"An error occurred: {e}")

json_data = [
    {"Name": "Prajwal", "Age": 24, "City": "Bengaluru"},
    {"Name": "Arnab", "Age": 25, "City": "Los Angeles"},
    {"Name": "", "Age": 35, "City": "Chicago"}
]

output_file = "output.xlsx"

json_to_excel(json_data, output_file)

Not only creating an Excel file easy in pandas, but also reading and looping, and there are many more capabilities of it. The main intention of this week’s techletter was to let you know that, if you are working with huge data go for Python programming and use pandas. Because they are already battle-tested.