Mayank Gupta

Python: Add a Column to an Existing CSV File

Methods to add a column to an existing CSV File

In this article, we will discuss how to add a column to an existing CSV file using csv.reader and csv.DictWriter  classes. Apart from appending the columns, we will also discuss how to insert columns in between other columns of the existing CSV file.

Original CSV file content

total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
  • Method 1-Add a column with the same values to an existing CSV file

In this, we see how we make one column and add it to our CSV file but all the values in this column are the same.

Steps will be to append a column in CSV file are,

  1. Open ‘input.csv’ file in read mode and create csv.reader object for this CSV file
  2. Open ‘output.csv’ file in write mode and create csv.writer object for this CSV file
  3. Using reader object, read the ‘input.csv’ file line by line
  4. For each row (read like a list ), append default text in the list.
  5. Write this updated list / row in the ‘output.csv’ using csv.writer object for this file.
  6. Close both input.csv and output.csv file.

Let see this with the help of an example

from csv import writer
from csv import reader
default_text = 'New column'
# Open the input_file in read mode and output_file in write mode
with open('example1.csv', 'r') as read_obj, \
        open('output_1.csv', 'w', newline='') as write_obj:
    # Create a csv.reader object from the input file object
    csv_reader = reader(read_obj)
    # Create a csv.writer object from the output file object
    csv_writer = writer(write_obj)
    # Read each row of the input csv file as list
    for row in csv_reader:
        # Append the default text in the row / list
        row.append(default_text)
        # Add the updated row / list to the output file
        csv_writer.writerow(row)
output_data=pd.read_csv('output_1.csv')
output_data.head()

Output

total_bill tip sex smoker day time size New column
0 16.99 1.01 Female No Sun Dinner 2 New column
1 10.34 1.66 Male No Sun Dinner 3 New column
2 21.01 3.50 Male No Sun Dinner 3 New column
3 23.68 3.31 Male No Sun Dinner 2 New column
4 24.59 3.61 Female No Sun Dinner 4 New column

Here we see that new column is added but all value in this column is same.

Now we see how we can add different values in the column.

  •  Method 2-Add a column to an existing CSV file, based on values from other columns

In this method how we can make a new column but in this column the value we add will be a combination of two or more columns. As we know there is no direct function to achieve so we have to write our own function to achieve this task. Let see the code for this.

from csv import writer
from csv import reader
def add_column_in_csv(input_file, output_file, transform_row):
    """ Append a column in existing csv using csv.reader / csv.writer classes"""
    # Open the input_file in read mode and output_file in write mode
    with open(input_file, 'r') as read_obj, \
            open(output_file, 'w', newline='') as write_obj:
        # Create a csv.reader object from the input file object
        csv_reader = reader(read_obj)
        # Create a csv.writer object from the output file object
        csv_writer = writer(write_obj)
        # Read each row of the input csv file as list
        for row in csv_reader:
            # Pass the list / row in the transform function to add column text for this row
            transform_row(row, csv_reader.line_num)
            # Write the updated row / list to the output file
            csv_writer.writerow(row)
add_column_in_csv('example1.csv', 'output_2.csv', lambda row, line_num: row.append(row[0] + '__' + row[1]))
output_data=pd.read_csv('output_2.csv')
output_data.head()

Output

total_bill tip sex smoker day time size total_bill__tip
0 16.99 1.01 Female No Sun Dinner 2 16.99__1.01
1 10.34 1.66 Male No Sun Dinner 3 10.34__1.66
2 21.01 3.50 Male No Sun Dinner 3 21.01__3.5
3 23.68 3.31 Male No Sun Dinner 2 23.68__3.31
4 24.59 3.61 Female No Sun Dinner 4 24.59__3.61

Here we see the new column is formed as the combination of the values of the 1st and 2nd column.

Explanation:

In the Lambda function, we received each row as a list and the line number. It then added a value in the list and the value is a merger of the first and second value of the list. It appended the column in the contents of example1.csv by merging values of the first and second columns and then saved the changes as output_2.csv files.

  • Method 3-Add a list as a column to an existing csv file

In this method, we will add our own value in the column by making a list of our values and pass this into the function that we will make. Let see the code for this.

from csv import writer
from csv import reader
def add_column_in_csv(input_file, output_file, transform_row):
    """ Append a column in existing csv using csv.reader / csv.writer classes"""
    # Open the input_file in read mode and output_file in write mode
    with open(input_file, 'r') as read_obj, \
            open(output_file, 'w', newline='') as write_obj:
        # Create a csv.reader object from the input file object
        csv_reader = reader(read_obj)
        # Create a csv.writer object from the output file object
        csv_writer = writer(write_obj)
        # Read each row of the input csv file as list
        for row in csv_reader:
            # Pass the list / row in the transform function to add column text for this row
            transform_row(row, csv_reader.line_num)
            # Write the updated row / list to the output file
            csv_writer.writerow(row)
l=[]
l.append("New Column")
rows = len(data.axes[0])
for i in range(rows):
    val=i+1
    l.append(val)
add_column_in_csv('example1.csv', 'output_3.csv', lambda row, line_num: row.append(l[line_num - 1]))
output_data=pd.read_csv('output_3.csv')
output_data.head()

Output

total_bill tip sex smoker day time size New Column
0 16.99 1.01 Female No Sun Dinner 2 1
1 10.34 1.66 Male No Sun Dinner 3 2
2 21.01 3.50 Male No Sun Dinner 3 3
3 23.68 3.31 Male No Sun Dinner 2 4
4 24.59 3.61 Female No Sun Dinner 4 5

Explanation

In the Lambda function, we received each row as a list and the line number. It then added a value in the list and the value is an entry from our list l at index  line_num – 1.Thus all the entries in the list l are added as a column in the CSV.

So these are some of the methods to add new column in csv.

Python: How to create a zip archive from multiple files or Directory

Method to create zip archive from multiple files or directory in python

In this article, we discuss how we can create a zip archive of multiple files or directories in python. To understand this let us understand about ZipFile class.

ZipFile class

To execute this program we have to import ZipFile class of zipfile module.ZipFile is a class of zipfile modules for reading and writing zip files.

syntax: zipfile.ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True)

Create a zip archives of multiples file

    • Method 1:Without using with statement

Let us first write the program and then see how code works.

from zipfile import ZipFile
# create a ZipFile object
zipObj = ZipFile('sample.zip', 'w')
# Add multiple files to the zip
zipObj.write('file1.txt')
zipObj.write('file2.txt')
# close the Zip File
zipObj.close()

Output

Directory structure before the execution of the program

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10-06-2021 18:41 14 file1.txt
-a---- 10-06-2021 18:41 15 file2.txt
-a---- 10-06-2021 19:06 216 zip.py

Directory structure after the execution of the program

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10-06-2021 18:41 14 file1.txt
-a---- 10-06-2021 18:41 15 file2.txt
-a---- 10-06-2021 19:09 239 sample.zip
-a---- 10-06-2021 19:09 216 zip.py

Here we clearly see that a zip file is created.

Let see how the program works. First, we create a ZipFile object bypassing the new file name and mode as ‘w’ (write mode). It will create a new zip file and open it within the ZipFile object. Then we use Call write() function on ZipFile object to add the files in it and then call close() on ZipFile object to Close the zip file.

  • Method 2:Using with statement

The difference between this and the previous method is that when we didn’t use with the statement then we have to close the zip file when the ZipFile object goes out of scope but when we use with statement the zip file automatically close when the ZipFile object goes out of scope. Let see the code for this.

from zipfile import ZipFile
# Create a ZipFile Object
with ZipFile('sample2.zip', 'w') as zipObj:
   # Add multiple files to the zip
   zipObj.write('file1.txt')
   zipObj.write('file2.txt')

Output

Directory structure before the execution of the program

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10-06-2021 18:41 14 file1.txt
-a---- 10-06-2021 18:41 15 file2.txt
-a---- 10-06-2021 19:09 239 sample.zip
-a---- 10-06-2021 19:21 429 zip.py

Directory structure after the execution of the program

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10-06-2021 18:41 14 file1.txt
-a---- 10-06-2021 18:41 15 file2.txt
-a---- 10-06-2021 19:09 239 sample.zip
-a---- 10-06-2021 19:24 239 sample2.zip
-a---- 10-06-2021 19:24 429 zip.py

Here we see that another zip file is created.

Create a zip archive of the directory

To zip selected files from a directory we need to check the condition on each file path while iteration before adding it to the zip file. As here we work on directory and file so we also have to import os module. Let see the code for this.

from zipfile import ZipFile
import os
from os.path import basename
# create a ZipFile object
dirName="../zip"
with ZipFile('sampleDir.zip', 'w') as zipObj:
   # Iterate over all the files in directory
   for folderName, subfolders, filenames in os.walk(dirName):
       for filename in filenames:
           #create complete filepath of file in directory
           filePath = os.path.join(folderName, filename)
           # Add file to zip
           zipObj.write(filePath, basename(filePath))

Output

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10-06-2021 18:41 14 file1.txt
-a---- 10-06-2021 18:41 15 file2.txt
-a---- 10-06-2021 19:09 239 sample.zip
-a---- 10-06-2021 19:24 239 sample2.zip
-a---- 10-06-2021 21:44 2796 sampleDir.zip
-a---- 10-06-2021 19:24 429 zip.py
-a---- 10-06-2021 21:44 506 zipdir.py

Here we see that the sampleDir.zip file is created.

Python : Create an Empty 2D Numpy Array and Append Rows or Columns to it

Create an empty 2-D NumPy array and append rows and columns

In this article, we will discuss what is a NumPy array, how to create a NumPy array in python, and how to append rows and columns to the empty NumPy array. First, let see what a NumPy array is and how we can create it. You can also create empty numpy array

NumPy

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it in our program and then we can easily use the functionality of NumPy in our program. Let us see how NumPy works with the help of an example.

import numpy as np

#0-D array
arr = np.array(1)
print(arr)
print(type(arr))
print()

#1-D array
arr_1d = np.array([1, 2, 3, 4, 5])
print(arr_1d)
print(type(arr_1d))
print()

#2-D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d)
print(type(arr_2d))

Output

1
<class 'numpy.ndarray'>

[1 2 3 4 5]
<class 'numpy.ndarray'>

[[1 2 3]
 [4 5 6]]
<class 'numpy.ndarray'>

Here we see how we can easily work with an n-dimensional array in python using NumPy.

Let us come to the main topic of the article i.e how to create an empty 2-D array and append rows and columns to it.

Create an empty NumPy array

To create an empty array there is an inbuilt function in NumPy through which we can easily create an empty array. The function here we are talking about is the .empty() function.

Syntax: numpy.empty(shape, dtype=float, order=‘C’)

It accepts shape and data type as arguments. Then returns a new array of given shapes and data types. Let us understand this with the help of an example.

array = np.empty((0, 4), int)
print(array)
print(type(array))

Output

[]
<class 'numpy.ndarray'>

This is the NumPy array consisting of 0 rows and 4 columns.

Now we understand how to create an empty 2-D NumPy array, now let us see how to append rows and columns to this empty array.

As we want to append rows and columns so there is also an inbuilt functioning NumPy to done this task and the method name is .append().

Syntax: numpy.append(arr, values, axis=None)

It contains 3 parameters. First is arr in which we have to pass our NumPy array, second is values i.e. what values we want to be appended in our NumPy array and 3rd is the axis. Axis along which values need to be appended. To append as row axis is 0, whereas to append as column it is 1.

Append rows to empty NumPy array

With the help of the append() method, we can be done this task but we need to take care of some points before using the append function.

  1.  As we append data row-wise so we need to pass axis=0.
  2. We have to pass the row to be appended as the same shape of the numpy array otherwise we can get an error i.e. as we have created an empty array with 4 columns so now we are restricted to use 4 elements in our NumPy array otherwise we will get an error.

Let us see how this function works with the help of an example.

array = np.empty((0, 4), int)
array = np.append(array, np.array([[1,2,3,4], [5,6,7,8]]), axis=0)
print(array)
type(array)

Output

[[1 2 3 4]
 [5 6 7 8]]
numpy.ndarray

Here we see with the help of append() we easily append rows in our empty 2-D NumPy array.

Append columns to empty NumPy array

With the help of the append() method, we can be done this task but here also we need to take care of some points before using the append function.

  1.  As we append data column-wise so we need to pass axis=1.
  2. We have to pass the column to be appended as the same shape of the numpy array otherwise we can get an error.

Let us see how this function works with the help of an example.

# Create an empty 2D numpy array with 4 rows and 0 column
array = np.empty((4, 0), int)

columns = np.array([[1,2,3,4], [5,6,7,8]])
array = np.append(array, columns.transpose(), axis=1)
print(array)

Output

[[1 5]
 [2 6]
 [3 7]
 [4 8]]

Here we see with the help of the append method how we are able to append columns to our empty 2-D NumPy array. In this example or method, we see that we use the transpose() function. So let us understand why the transpose() function is used here.

transpose() Function

Here transpose() has the same functionality that the transpose of a matrix in mathematics. Here we see that we create our array row-wise but we want to enter them in the .append() function column-wise. Hence transpose is used to swap rows and columns.

Python: Find Unique Values in a Numpy Array With Frequency and Indices

Methods to find unique values in a numpy array with frequency and indices

In this article, we will discuss how to find unique values, rows, and columns in a 1D & 2D Numpy array. Before going to the methods first we see numpy.unique() method because this method is going to be used.

numpy.unique() method

numpy.unique() method help us to get the unique() values from given array.

syntax:numpy.unique(array, return_index=False, return_inverse=False, return_counts=False, axis=None)

Parameters

  1. array-Here we have to pass our array from which we want to get unique value.
  2. return_index- If this parameter is true then it will return the array of the index of the first occurrence of each unique value. By default it is false.
  3. return_counts-If this parameter is true then it will return the array of the count of the occurrence of each unique value. By default it is false.
  4. axis- It is used in the case of nd array, not in 1d array. axis=1 means we have to do operation column-wise and axis=0 means we have to do operation row-wise.

Now we will see different methods to find unique value with their indices and frequencies in a numpy array.

case 1-When our array is 1-D

  • Method 1-Find unique value from the array

As we only need unique values and not their frequencies and indices hence we simply pass our numpy array in the unique() method because the default value of other parameters is false so we don’t need to change them. Let see this with the help of an example.

import numpy as np
arr = np.array([1, 1, 2, 3, 4, 5, 6, 7, 2, 3, 1, 4, 7])
unique_values=np.unique(arr)
print("Original array is")
print(arr)
print("------------------")
print("Unique values are")
print(unique_values)

Output

Original array is
[1 1 2 3 4 5 6 7 2 3 1 4 7]
------------------
Unique values are
[1 2 3 4 5 6 7]
  • Method 2-Find unique value from the array along with their indices

In this method, as we want to get unique values along with their indices hence we make the return_index parameter true and pass our array. Let see this with the help of an example.

import numpy as np
arr = np.array([1, 1, 2, 3, 4, 5, 6, 7, 2, 3, 1, 4, 7])
unique_values,index=np.unique(arr,return_index=True)
print("Original array is")
print(arr)
print("------------------")
print("Unique values are")
print(unique_values)
print("First index of unique values are:")
print(index)

Output

Original array is
[1 1 2 3 4 5 6 7 2 3 1 4 7]
------------------
Unique values are
[1 2 3 4 5 6 7]
First index of unique values are:
[0 2 3 4 5 6 7]
  • Method 3-Find unique value from the array along with their frequencies

In this method, as we want to get unique values along with their frequencies hence we make the return_counts parameter true and pass our array. Let see this with the help of an example.

import numpy as np
arr = np.array([1, 1, 2, 3, 4, 5, 6, 7, 2, 3, 1, 4, 7])
unique_values,count=np.unique(arr,return_counts=True)
print("Original array is")
print(arr)
print("------------------")
print("Unique values are")
print(unique_values)
print("Count of unique values are:")
for i in range(0,len(unique_values)):
  print("count of ",unique_values[i]," is ",count[i])

Output

Original array is
[1 1 2 3 4 5 6 7 2 3 1 4 7]
------------------
Unique values are
[1 2 3 4 5 6 7]
Count of unique values are:
count of  1  is  3
count of  2  is  2
count of  3  is  2
count of  4  is  2
count of  5  is  1
count of  6  is  1
count of  7  is  2

Case 2: When our array is 2-D

  • Method 1-Find unique value from the array

Here we simply pass our array and all the parameter remain the same. Here we don’t make any changes because we want to work on both rows and columns. Let see this with the help of an example.

import numpy as np
arr = np.array([[1, 1, 2,1] ,[ 3, 1, 2,1] , [ 6, 1, 2, 1],  [1, 1, 2, 1]])
unique_values=np.unique(arr)
print("Original array is")
print(arr)
print("------------------")
print("Unique values are")
print(unique_values)

Output

Original array is
[[1 1 2 1]
 [3 1 2 1]
 [6 1 2 1]
 [1 1 2 1]]
------------------
Unique values are
[1 2 3 6]

Method 2-Get unique rows

As here want to want to work only on rows so here we will make axis=0 and simply pass our array. Let see this with the help of an example.

import numpy as np
arr = np.array([[1, 1, 2,1] ,[ 3, 1, 2,1] , [ 6, 1, 2, 1],  [1, 1, 2, 1]])
unique_values=np.unique(arr,axis=0)
print("Original array is")
print(arr)
print("------------------")
print("Unique rows are")
print(unique_values)

Output

Original array is
[[1 1 2 1]
 [3 1 2 1]
 [6 1 2 1]
 [1 1 2 1]]
------------------
Unique rows are
[[1 1 2 1]
 [3 1 2 1]
 [6 1 2 1]]

Method 3-Get unique columns

As here want to want to work only on columns so here we will make axis=1 and simply pass our array. Let see this with the help of an example.

import numpy as np
arr = np.array([[1, 1, 2,1] ,[ 3, 1, 2,1] , [ 6, 1, 2, 1],  [1, 1, 2, 1]])
unique_values=np.unique(arr,axis=1)
print("Original array is")
print(arr)
print("------------------")
print("Unique columns are")
print(unique_values)

Output

Original array is
[[1 1 2 1]
 [3 1 2 1]
 [6 1 2 1]
 [1 1 2 1]]
------------------
Unique columns are
[[1 1 2]
 [1 3 2]
 [1 6 2]
 [1 1 2]]

so these are the methods to find unique values in a numpy array with frequency and indices.

 

Python: Count Number of True Elements in Numpy Array in Python

Method to find count number of true element in numpy array in python

In this article, we will study what are true elements in python and different methods to calculate the count of true elements in both 1-D and 2-D Numpy array. First, let see what are true elements in python.

True Elements

True elements in numpy array are equivalent to 1 and False element is equivalent to 0. So we can say that it is basically a boolean numpy array.

Now let us see different methods to count a number of True elements in a 1-D numpy array.

  • Method 1-Using count_nonzero() method

This is one of the methods to count a number of true elements in a numpy array. count_nonzero() method require 1 argument i.e. the name of numpy array and then it return the count of true element in numpy array. It also accepts axis as one of the arguments but we do not need it in this case.

Syntax: count_nonzero(arr, axis=None)

Let see this with the help of an example.

import numpy as np

arr1 = np.array([False, True, True, False,True])
arr2 = np.array([0,1,1,0,1])
count1 = np.count_nonzero(arr1)
count2 = np.count_nonzero(arr2)
print('Print count of True elements in array1: ', count1)
print('Print count of True elements in array2: ', count2)

Output

Print count of True elements in array1:  3
Print count of True elements in array2:  3

There are two things to notice in this example first is that in arr1 count of True is 3 hence we get output as 3 and the second thing to notice is that if we write 1 and 0 instead of True and False respectively then we will get the same output. Hence it is also verified that True is equivalent to 1 and False is equivalent to 0 in a numpy array.

  • Method 2-Using sum() function

sum() is another method to count a number of true elements in a numpy array. Here sum() method works in the same way that we see in basic python. It returns the sum of all the elements in a Numpy array. As True is equivalent to 1 and False is equivalent to 0 so the sum we get is equal to count of True elements.

syntax: sum(arr)

Let see this with the help of an example.

import numpy as np

arr1 = np.array([False, True, True, False,True])
count1 = sum(arr1)
print('Print count of True elements in array1: ', count1)

Output

Print count of True elements in array1:  3
  • Method 3 Using bincount() method

bincount() return list of occurrence of each value in numpy array. Let see this with an example.

import numpy as np

arr1 = np.array([False, True, True, False,True])
l = np.bincount(arr1)
print(l)

Output

[2 3]

Here we see that at index 0 it return the count of the number of false values and at index 1 it return a count of true values. So now using list indexing we can return the count of elements at the 1st index which is the count of True values in the Numpy array.

So these are the methods to count a number of True values in a 1-D Numpy array.

Now we see that how to calculate the count of numpy array in the 2-D array. The best part is that the above methods that we study are used to calculate the count of True elements in a 2-D numpy array but the difference is that we can give an extra parameter as the axis to calculate the count of True elements row-wise or column-wise.

  • Method 1-Using count_nonzero() method

The working of the method is the same that we study in the case of a 1-D numpy array. But here we have a choice that we can give the axis as a parameter or not. If we do not give axis as a parameter then this method will return the total count of True elements in a numpy array but if we give an axis it returns a list of the count of True element row-wise or column-wise. There are two axis

axis=1 means return count row-wise

axis=0 means return count column-wise

Let see this with the help of an example.

import numpy as np

arr_2d = np.array([ [False, True, True,False],
                    [True, False, True,False],
                    [False, True, True,False]])
count1 = np.count_nonzero(arr_2d)
count2 = np.count_nonzero(arr_2d,axis=1)
count3 = np.count_nonzero(arr_2d,axis=0)
print("Total count of True values",count1)
print("Count of True values row-wise",count2)
print("Count of True values column-wise",count3)

Output

 

Total count of True values 6
Count of True values row-wise [2 2 2]
Count of True values column-wise [1 2 3 0]
  • Method 2-Using sum() method

The working of the method is the same that we study in the case of a 1-D numpy array. But here we have a choice that we can give the axis as a parameter or not. If we do not give axis as a parameter then this method will return the total count of True elements in a numpy array but if we give an axis it returns a list of the count of True element row-wise or column-wise. There are two axis

axis=1 means return count row-wise

axis=0 means return count column-wise

Here we have to take care of one this that we can’t simply write sum() here we have to write np.sum() because we use axis as one of argument.

Let see this with the help of an example.

import numpy as np

arr_2d = np.array([ [False, True, True,False],
                    [True, False, True,False],
                    [False, True, True,False]])
count1 = np.sum(arr_2d)
count2 = np.sum(arr_2d,axis=1)
count3 = np.sum(arr_2d,axis=0)
print("Total count of True values",count1)
print("Count of True values row-wise",count2)
print("Count of True values column-wise",count3)

Output

Total count of True values 6
Count of True values row-wise [2 2 2]
Count of True values column-wise [1 2 3 0]

So these are the methods to count the number of True values in both 1-D and 2-D numpy array.

Create NumPy Array from List, Tuple or List of Lists in Python

Methods to create NumPy array from list, tuple, or list of lists in Python

In this article, we will discuss some basics of NumPy array and how to create NumPy array from lists, tuples, and Lists of lists in python.

NumPy

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it in our program and then we can easily use the functionality of NumPy in our program.

numpy.array()

This method is going to be widely used in our program so it will be beneficial to study this method in advance so that it will be easy for us to understand the concepts. This method helps us to create a numpy array from already defined data structures in python like lists, tuples, and nested lists.

Syntax: numpy.array(object, dtype=None, copy=True, order=‘K’, subok=False, ndmin=0)

This method returns a numpy array.

numpy.asarray()

This method helps us to create a numpy array from already defined data structures in python like lists, tuples, and nested lists.

Syntax: numpy.asarray(arr, dtype=None, order=None)

Difference between numpy.array() and numpy.asarray()

The major difference between both the methods is that numpy.array() will make a duplicate of original copy while the numpy.asarray() make changes in the original copy.

Create a numpy array from a list

  • Method 1-Using numpy.array()

In this method, we will see how we can create a numpy array from the list in python. When we look at the parameter of this method we see that in the parameter one parameter is an object that clearly gives us a hint that we can pass our list in this method and we may get our output in the form of NumPy array. Let us see this method with the help of an example.

l=[1,2,3,4,5,6]
array=np.array(l)
print(array)
print(type(array))

Output

[1 2 3 4 5 6]
<class 'numpy.ndarray'>

Here we see how we easily create our numpy array from the list using numpy.array() method.

  • Method 2-Using numpy.asarray()

This method also works in the same way as numpy.array() work.We just simply pass our list object in the function and we will get our numpy array. Let see this with an example.

l=[1,2,3,4,5,6]
array=np.asarray(l)
print(array)
print(type(array))

Output

[1 2 3 4 5 6]
<class 'numpy.ndarray'>

Create a numpy array from a tuple

  • Method 1-Using numpy.array()

Here the procedure is the same as we discuss in the case of lists. Here instead of a list, we have to pass out a tuple and we get our numpy array. Let see this with the help of an example.

t=(1,2,3,4,5,6)
array=np.array(t)
print(array)
print(type(array))

Output

[1 2 3 4 5 6]
<class 'numpy.ndarray'>
  • Method 2-Using numpy.asarray()

Just like we use this method in the case of lists here we also use this in a similar way. But here instead of using the list, we pass tuple as an object. Let see this with the help of an example.

t=(1,2,3,4,5,6)
array=np.asarray(t)
print(array)
print(type(array))

Output

[1 2 3 4 5 6]
<class 'numpy.ndarray'>

Create a 2d Numpy array from a list of list

numpy. array() method will also work here and the best part is that the procedure is the same as we did in the case of a single list.In this case, we have to pass our list of lists as an object and we get our output as a 2d array.Let see this with the help of an example.

l=[[1,2,3],[4,5,6]]
array=np.asarray(l)
print(array)
print(type(array))

Output

[[1 2 3]
 [4 5 6]]
<class 'numpy.ndarray'>

So these are the methods to create numpy array from list,tuples and lists of lists.

Python: if-else in One Line/Ternary Operator in Python

How to write an if-else statement in one line in python

In this article, we will discuss how we can implement the if-else statement in python in a single line. This is also called the ternary operator. In python, the ternary operator doesn’t have syntax like that in c++ or java but working is the same. Let us see this concept with some examples.

Syntax: val1 if [expression] else val2

Explanation: If our if the statement becomes true then we get output as val1 otherwise we get output as val2.Let us see this with an example.

a=10
print(1 if a%2==0 else 0)
a=9
print(1 if a%2==0 else 0)

Output

1
0

Here we see that we get output 1 when our number is even else we get output as 0. When the condition evaluates to True, then the result of this one-liner if..else expression will be val1 in this case 1. Whereas, if the condition evaluates to False, then the result of this one-liner expression will be val2 in this case 0.

Here we use integer with our if-else statement. Let us see an example of how to deal with a string.

a=10
print("Number is even" if a%2==0 else "Number is odd")

Output

Number is even

Here we see that how we easily deal with string with this ternary operator or one-liner if-else statement. In a similar way, we can deal with different data types.

With the increase in the simplicity of code, we also have to take some precaution otherwise a situation may arise in which we understand the code in a different way while the code work in a different way. As different operator have different associativity and precedence which increase the responsibility of the programmer to use this operator in a correct way. Let us understand the concept with help of an example.

a=10 
b=5
print(a/b*3 if a%2==0 else 1)
print(a/(b*3 if a%2==0 else 1))

Output

6.0
0.6666666666666666

Here we see with change in one bracket our output differ but code seems to be similar.This confusion tend to be increase when we write big and complex code.Hence we have to use operator in a proper way.

Note: Conditional expressions have the lowest priority amongst all Python operations.

Nested if-else in one line

We see that we can execute single if-else in a single line easily.Now we see how we can execute multiple if else statement in a single line with the help of an example.

a=10 
print("a is negative" if a<0 else "a is positive" if a>0 else "a is 0")

#above code is similar as
if(a<0):
    print("a is negative")
else:
    if(a>0):
        print("a is positive")
    else:
        print("a is 0")

Output

a is positive
a is positive

Both the code give a similar result. We see how we save 5-6 lines by using the ternary operator.

So in this article, we see how to use the if-else and nested if-else statement in one line and with that, we see how to use the operator effectively otherwise the result may differ.

Python : Join / Merge Two or More Lists

Basic Concepts/Terminologies

In this article, we will discuss different ways to join two or more lists in python. Before going to the actual let us understand about lists in python.

List

The list is one of the data structures in python that is used to store multiple items in a single variable. The best thing about the list is that we can store the value of different data types in a single list i.e. we can store string, int, and float values in the same list.

l=[1,2,"list",3.3,"Raj",4]
print(type(l))

Output

<class 'list'>

In this example, we see how we can easily store different variables in the same list.

Ways to join two or more lists in python

  • Method 1-Using the “+” operator

Here the role of the “+” operator is the same as in maths. The “+” operator can easily join/merge two or more lists like we add two or more numbers in maths. In python, we called this concept concatenation. Let us see this concept with the help of an example.

l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
new_l=l1+l2
print(new_l)

Output

[1, 2, 'list1', 3.3, 'Raj', 4, 8, 1.1, 'list2']

Here we see that how we easily concatenate our lists using the “+” operator. The second thing to notice here is that the list that comes before the “+” operator has also come before another list in the new list.

  • Method 2-Using for loop

This is also one of the methods to merge two or more lists in python. In this method, we iterate through all the elements in one list and append these elements in the second list. Fore appending element in the second list we can use the append() method which is an inbuilt method in a python list. Let us see this method with the help of an example.

l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
for item in l1:
    l2.append(item)
print(l2)

Output

[8, 1.1, 'list2', 1, 2, 'list1', 3.3, 'Raj', 4]
  • Method 3-Using list.extend()

extend() method is used to add all the elements of an iterable i.e. list, tuple, etc at the end of the list. The best thing about this method is that we don’t need to use it for loops. we simply pass our iterable as an argument in extend() function and our task will be done. This is an in-place method that means it made changes in an existing list instead of creating a new list.

syntax: list.extend(anotherList)

Let us see this method with the help of an example.

l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
l1.extend(l2)
print(l1)

Output

[1, 2, 'list1', 3.3, 'Raj', 4, 8, 1.1, 'list2']
  • Method 4-Using list comprehensions

Before understanding this method let us take a quick look at what list comprehension is.

List comprehensions

List comprehension is a way of creating a new list from the existing list. It helps in solving the problem related to list in one line.

Let us see how to join two lists using list comprehension with the help of an example.

l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
l = [y for x in [l1, l2] for y in x]
print(l)

Output

[1, 2, 'list1', 3.3, 'Raj', 4, 8, 1.1, 'list2']

Here we how we easily join our two lists using list comprehension.

  • Method 5- Using itertools.chain()

Before understanding this method let us see what an itertools is.

itertools

It is a module in python that provides methods to work flexibly with different iterables in python. To use this module we have to import this using the import keyword in our program.

itertools.chain()

This method takes multiple iterables at a one-time group them and produces a single iterable as output.

syntax-itertools.chain(*iterables)

Here * indicates that we can pass multiple iterable as a parameter in this function.

Let us see this concept with the help of an example.

import itertools
l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
l=itertools.chain(l1, l2)
merge_list=list(l)
print(l)
print(merge_list)

Output

<itertools.chain object at 0x000002A9837445F8>
[1, 2, 'list1', 3.3, 'Raj', 4, 8, 1.1, 'list2']

Explanation

This method returns a chain object as we see in the output. We convert this chain object to a list to get our desired output.

  • Method 6- Using unpacking or “*” operator

As the name suggests unpacking or “*:” is used to unpack the content of any iterables object. Hence we can unpack the content of two or more lists and then merge them to form a new list.

Note: This method works only in python3.6+ versions.

l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
l = [*l1, *l2]
print(l)

Output

[1, 2, 'list1', 3.3, 'Raj', 4, 8, 1.1, 'list2']

So these are the methods to append two or more lists in python.

How to Create a Directory in Python?

In this article, we will discuss how to create a directory in python.

Directory

The directory or simply say folder is a collection of files and sub-directory. A directory or folder is created to store our files in an organized manner.

Ways to create Directory in python

Using the Python os module we can easily work with directories. Let us see different ways to create a directory or folder in python.

  • Method 1-using os. mkdir()

First, let us discuss os. mkdir() function or method because this is the base of this whole concept.

os.mkdir()

This method is used to create a directory with a name path and a specified numeric mode. The default mode is 0777(octal). But we don’t need to focus on the mode part.

syntax:  os.mkdir(path[, mode])  or os.mkdir(path)

Here the path is the path which we need to be created. For example, C:\Users\HP\Desktop\python this is a path. Let us see how we can make a directory with this method.

For creating a directory with this method we have to give the name of our directory which we want to be created in the mkdir() method as an argument. In this way, our directory will be created in the current directory. Let us see this with the help of an example.

import os
print("Original directory Structure")
print(os.listdir())
#create new directory with name myDir
os.mkdir("myDir")
print("New directory structure")
print(os.listdir())

Output

Original directory Structure
['.vscode', '02_railway_oops.py', 'directory.py', 'file_handling.py', 'first.cpp', 'first.exe', 'hello.py', 'HelloWorld.class', 'newDir'] 
New directory structure
['.vscode', '02_railway_oops.py', 'directory.py', 'file_handling.py', 'first.cpp', 'first.exe', 'hello.py', 'HelloWorld.class', 'myDir', 'newDir']

Explanation:

In this example, we clearly see that first, we have no directory name “myDir” in the current directory but after we use os. mkdir() function our directory is created.

Note: If the directory already exists in the current directory then it will raise FileExistsError Error. To cope with this exception we can use try-except block.

  • Method 2-using os. makedirs()

First, let us discuss os. makedirs() function or method because this is the base of this whole concept.

os.makedirs()

This method is used to create a directory recursively with a name path and a specified numeric mode. But we don’t need to focus on the mode part.

For example, suppose we want to make a directory with this path /home/User/Documents/dir1/dir2 but in this path our directory Documents and dir1 not exist then it will not throw any error. It will create that missing directory also. The working will be the same as that of os. mkdir()

syntax:  os.makedirs(path[, mode])  or os.makedirs(path)

import os
print("Original directory Structure")
print(os.listdir())
#create new directory with name makeDir
os.makedirs("makeDir")
print("New directory structure")
print(os.listdir())

Output

Original directory Structure
['.vscode', '02_railway_oops.py', 'directory.py', 'file_handling.py', 'first.cpp', 'first.exe', 'hello.py', 'HelloWorld.class', 'myDir', 'newDir']
New directory structure
['.vscode', '02_railway_oops.py', 'directory.py', 'file_handling.py', 'first.cpp', 'first.exe', 'hello.py', 'HelloWorld.class', 'makeDir', 
'myDir', 'newDir']

So by these methods, we can create a directory in python.

How to Get the Path of Current Working Directory in Python?

Get the path of the current working directory in Python

In this article, we will discuss how to get the path of the current working directory in python. First, let us understand what a directory is.

Directory

The directory or simply say folder is a collection of files and sub-directory. A directory or folder is created to store our files in an organized manner.

Path of Directory

The path of the directory is specified as a unique location of the directory in the system. Unique here identifies that no two files or directories can have the same name in the same path or location. There are 2 types of path one is an absolute path while the other is a relative path.

Absolute path

The path with reference to the root directory is called the absolute path.

Relative Path

The path with reference to the current directory is called the relative path.

os.getcwd() Method

This can be achieved through the os module in python. Before using this module we have to import this module into the program and then we are free to use os module functions.

getcwd() function of os module helps us to find the path current working directory. As this function returns the path of current working directly so we can either store it in a variable then print it or we can directly print it as per our requirement.

syntax: variable_name = os.getcwd()

import os
print(os.getcwd())

Output

C:\Users\HP\Desktop\python

So we can easily get the path of our current working directly using this method.

Now the question that may arise is this path is an absolute path or a relative path. As we get the path with respect to the root directory hence os. getcwd() method gives the absolute path.