Python map() function

Map() function :- map() function in python used to perform operations on a given number of iterable sequence like list, set, tuple etc. map() function takes a function as an one argument and a iterable sequence as a second argument.  map(func, iter) func:- This is a function on which map function passes each element of given iterator. iter:- This is a iterable sequence like lists, sets, tuples etc. map() function returns a map object. so you have to change the type of result to list or tuple as per your requirement.…

How to connect with redshift in python

Connecting to AWS Redshift with Python The following Python code demonstrates how to establish a connection to an AWS Redshift database using the psycopg2 library. This function sets up a connection and creates a cursor for querying the database. import psycopg2 import psycopg2.extras def redshift_connect(): # Connection and session creation print(“redshift_connect start…”) conn = psycopg2.connect( dbname=”RS_DB_NAME”, host=”HOST_NAME”, port=”RS_PORT”, user=”USER_NAME”, password=”PWD” ) print(“redshift connection variable”, conn) cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) return conn, cursor This code defines a redshift_connect function that: Imports the psycopg2 library and its extras module for advanced cursor functionality.…

How to Convert a Python Dictionary to List

>>> from functools import reduce >>> a = {‘foo’: ‘bar’, ‘baz’: ‘quux’, ‘hello’: ‘world’} >>> list(reduce(lambda x, y: x + y, a.items())) [‘foo’, ‘bar’, ‘baz’, ‘quux’, ‘hello’, ‘world’] explanation:  -> a.items() returns a list of tuples.  -> Adding two tuples together makes one tuple containing all elements. Thus the reduction creates one tuple containing all keys and values and then the list(…) makes a list from that.

How to get data from redshift table as dictionary in python

The dict cursors allow to access to the retrieved records using an interface similar to the Python dictionaries instead of the tuples. Add following code into your cursor function cursor_factory=psycopg2.extras.RealDictCursor After adding the above code your cursor should be look like this – cur = con.cursor(cursor_factory=psycopg2.extras.RealDictCursor) if you have an error of not found class .extra – AttributeError: module ‘psycopg2’ has no attribute ‘extras’ Resolved this error by importing class as below import psycopg2 import psycopg2.extras Now if you run this code, you will get data in dictionary from redshift …

Pandas- ImportError: Missing required dependencies [‘numpy’]

This is a common error that sometimes occur when importing pandas library. Here I am sharing the solution for that. So please follow as I described below and if you found any difficulty, intimate me by your comment. Problem:- >>> import pandas Traceback (most recent call last):   File “<stdin>”, line 1, in <module>   File “/home/anil/anaconda3/lib/python3.6/site-packages/pandas/__init__.py”, line 19, in <module>     “Missing required dependencies {0}”.format(missing_dependencies)) ImportError: Missing required dependencies [‘numpy’] Solution:- check all folder list in your python lib folder -> ls -la Now if you see numpy…