Pandas – ValueError: If using all scalar values, you must pass an index

Posted Leave a commentPosted in Pandas, Python

Reading json file using Pandas read_json can fail with “ValueError: If using all scalar values, you must pass an index”. Let see with an example – cat a.json { “creator”: “CaptainAmerica”, “last_modifier”: “NickFury”, “title”: “Captain America: The First Avenger”, “view_count”: 12000 } >>> import pandas as pd >>> import glob >>> for f in glob.glob(‘*.json’): […]

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’, […]