Found 10476 Articles for Python

Create a Series from a List, Numpy Array, and Dictionary in Pandas

AmitDiwan
Updated on 15-Sep-2022 12:44:36

2K+ Views

Pandas is an open-source Python Library providing high-performance data manipulation and analysis tool using its powerful data structures. The name Pandas is derived from the word Panel Data – an Econometrics from Multidimensional data. Series is a one-dimensional labelled array capable of holding data of any type i.e. integer, string, float, python objects, etc). The axis labels are collectively called index. To create a series, at first install the pandas library. We use pip to install any library in Python − pip install pandas Create a Pandas Series from a List Example We will create a series from a ... Read More

Write a Code to Sort an Array in Numpy by the (N-1)Th Column

AmitDiwan
Updated on 15-Sep-2022 12:40:56

189 Views

In this example, we will see how to sort an array in Numpy by the (N-1)th column. Sort an array by (n-1) th column using argsort() Example Let us see the first example to sort an array by (n-1)th column − import numpy as np # Creat a Numpy Array a = np.array([[9, 2, 3], [4, 5, 6], [7, 0, 5]]) print("Array = ", a) # The value of n n = 3 # Sort by n-1 column print("Sort by n-1 th column = ", a[a[:, (n-1)].argsort()]) Output Array ... Read More

Difference Between range() and xrange() Functions in Python?

AmitDiwan
Updated on 15-Sep-2022 12:39:25

6K+ Views

The range() method in Python is used to return a sequence object. It is used in Python 3.x The xrange() is used to generate sequence of number and used in Python 2.x. Therefore, there’s no xrange() in Python 3.x. Let us first learn about range() and xrange() one by one. The range() method in Python The range() method returns a sequence of numbers and has 3 parameters i.e. start, stop and step. Here’s the syntax − range(start, stop, step) Here, start − Integer specifying at which position to start. stop − Integer specifying at which position to stop. ... Read More

How Can You Copy Objects in Python?

AmitDiwan
Updated on 15-Sep-2022 12:35:26

2K+ Views

In Python, if you want to copy object, the assignment operator won’t fulfil the purpose. It creates bindings between a target and an object i.e., it never creates a new object. It only creates a new variable sharing the reference of the original object. To fix this, the copy module is provided. This module has generic shallow and deep copy operations. Shallow Copy A shallow copy constructs a new compound object and then inserts references into it to the objects found in the original. It uses the following method to copy objects  − copy.copy(x) Return a shallow copy of x. ... Read More

Difference Between Matrices and Arrays in Python?

AmitDiwan
Updated on 15-Sep-2022 12:31:57

4K+ Views

The arrays in Python are ndarray objects. The matrix objects are strictly 2-dimensional whereas the ndarray objects can be multi-dimensional. To create arrays in Python, use the Numpy library. Matrices in Python Matrix is a special case of two-dimensional array where each data element is of strictly same size. Matrices are a key data structure for many mathematical and scientific calculations. Every matrix is also a two-dimensional array but not vice versa. Matrix objects are a subclass of ndarray, so they inherit all the attributes and methods of ndarrays. Example Create a Matrix and Display from numpy import * ... Read More

Python - Display the Contents of a Text File in Reverse Order?

AmitDiwan
Updated on 15-Sep-2022 12:27:46

5K+ Views

We will display the contents of a text file in reverse order. For that, let us first create a text file amit.txt with the following content  Display the contents of a text file in Reverse Order with Slicing Example Let us now read the contents of the above file in reverse order − # The file to be read with open("amit.txt", "r") as myfile: my_data = myfile.read() # Reversing the data by passing -1 for [start: end: step] rev_data = my_data[::-1] # Displaying the reversed data print("Reversed data = ", rev_data) Output Reversed ... Read More

Difference Between Del and Remove() on Lists in Python?

AmitDiwan
Updated on 15-Sep-2022 12:26:34

9K+ Views

Let us understand what is Del and Remove() in a Python List before moving towards the difference. Del Keyword in Python List The del keyword in Python is used to remove an element or multiple elements from a List. We can also remove all the elements i.e. delete the entire list. Example Delete an element from a Python List using the del keyword #Create a List myList = ["Toyota", "Benz", "Audi", "Bentley"] print("List = ", myList) # Delete a single element del myList[2] print("Updated List = ", myList) Output List = ['Toyota', 'Benz', 'Audi', 'Bentley'] Updated ... Read More

Replace All Occurrences of a Python Substring with a New String?

AmitDiwan
Updated on 15-Sep-2022 12:24:19

286 Views

In this article, we will implement various example to replace all occurrences of a substring with a new string. Let’s say we have the following string − Hi, How are you? How have you been? We have to replace all occurrences of substring “How “ with “Demo”. Therefore, the output should be − Hi, Demo are you? Demo have you been? Let us now see some examples − Replace All Occurrences of a Python Substring with a New String using replace() Example In this example, we will use the replace() method to replace all occurrences of a Python Substring with ... Read More

How to Merge Elements in a Python Sequence?

AmitDiwan
Updated on 15-Sep-2022 12:19:18

2K+ Views

Python Sequences includes Strings, Lists, Tuples, etc. We can merge elements of a Python sequence using different ways. Merge Elements in a Python List Example The join() method is used to merge elements − # List myList = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U'] # Display the List print ("List = " + str(myList)) # Merge items using join() myList[0 : 3] = [''.join(myList[0 : 3])] # Displaying the Result print ("Result = " + str(myList)) Output List = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U'] Result = ['HOW', 'A', 'R', ... Read More

Python - Check If All the Characters in a String Are Alphanumeric?

AmitDiwan
Updated on 15-Sep-2022 12:17:18

517 Views

To check if all the characters in a string are alphanumeric, we can use the isalnum() method in Python and Regex also. First, let us understand what is alphanumeric. What is Alphanumeric? Alphanumeric includes both letters and numbers i.e., alphabet letter (a-z) and numbers (0-9). Examples: A, a, k, K, 8, 10, 20, etc. Let us see an example of an Alphanumeric string 8k9q2i3u4t Let us see an example of a string wherein all the characters aren’t Alphanumeric - $$###k9q2i3u4t Check If All the Characters in a String Are Alphanumeric using isalnum() We will use the built-in isalnum() ... Read More

Advertisements