Merge json files using Pandas

Posted Leave a commentPosted in Coding, Pandas

Quick demo for merging multiple json files using Pandas – import pandas as pd import glob import json file_list = glob.glob(“*.json”) >>> file_list [‘b.json’, ‘c.json’, ‘a.json’] Use enumerate to assign counter to files. allFilesDict = {v:k for v, k in enumerate(file_list, 1)} >>> allFilesDict {1: ‘b.json’, 2: ‘c.json’, 3: ‘a.json’} Append the data into list […]

Python – sort() vs sorted(list)

Posted 3 CommentsPosted in Coding, Python

You can compare list using sort() or sorted(list), but be careful with sort() – >>> c = [(‘d’,4), (‘c’,3), (‘a’,1), (‘b’, 2)] >>> a = [(‘a’,1), (‘b’, 2), (‘c’,3), (‘d’,4)] >>> a.sort() == c.sort() True >>> >>> a = [(‘a’,1), (‘b’, 2), (‘c’,3), (‘d’,4)] >>> b = [(‘b’,2), (‘c’, 3), (‘a’,1)] >>> >>> a.sort() == […]

Python – str.maketrans()

Posted Leave a commentPosted in Coding, Python

Working on a Python code, I had a requirement for removing the single/double quotes and open/close brackets from the string of below format — >>> text = “””with summary as (select ‘ … ‘p.col1,p.col2,p.col3, ROW_NUMBER() ‘ … ‘OVER(PARTITION BY p.col1,p.col3 ORDER BY ‘ … ‘p.col2) AS rk from (select * from (select ‘ … ‘col2, […]

Python List

Posted Leave a commentPosted in Coding, Python

This blog post is about appending data elements to list in Python. Suppose we have a simple list “x”, we will look at different ways to append elements to this list. x = [1, 2, 3] The “append” method appends only a single element >>> x [1, 2, 3] >>> x.append(4) >>> x [1, 2, […]

Python – Flatten List of Lists

Posted Leave a commentPosted in Coding, Python

Itertools is one of the most powerful module in Python. Today I had requirement to flatten list of lists and itertools made it so easy. My list — >> val = [[‘a’,’b’],’c’,[‘d’,’e’,’f’]] Required Result [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’] How do you do it? Itertools to the resuce — >>> list(chain.from_iterable(val)) [‘a’, ‘b’, ‘c’, […]