Programming Articles - Page 1068 of 3363

Python Program to Remove First Diagonal Elements from a Square Matrix

AmitDiwan
Updated on 16-Sep-2021 08:09:00

237 Views

When it is required to remove the first diagonal elements from a square matrix, the ‘enumerate’ and list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [[45, 67, 85, 42, 11], [78, 99, 10, 13, 0], [91, 23, 23, 64, 23], [91, 11, 22, 14, 35]] print("The list is :") print(my_list) my_result = [] for index, element in enumerate(my_list): my_result.append([ele for index_1, ele in enumerate(element) if index_1 != index]) print("The resultant matrix is :") print(my_result)OutputThe list is : [[45, 67, 85, 42, 11], [78, 99, 10, 13, 0], [91, 23, 23, ... Read More

Python Program to Extract Strings with at least given number of characters from other list

AmitDiwan
Updated on 16-Sep-2021 08:07:26

163 Views

When it is required to extract strings with atleast a given number of characters from the other list, a list comprehension is used.ExampleBelow is a demonstration of the samemy_list = ["Python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) my_char_list = ['e', 't', 's', 'm', 'n'] my_key = 2 print("The value of key is ") print(my_key) my_result = [element for element in my_list if sum(ch in my_char_list for ch in element) >= my_key] print("The resultant list is :") print(my_result)OutputThe list is : ['Python', 'is', 'fun', 'to', 'learn'] The value of key is 2 The ... Read More

Python Pandas – Get the datatype and DataFrame columns information

AmitDiwan
Updated on 16-Sep-2021 08:10:31

259 Views

To get the datatype and DataFrame columns information, use the info() method. Import the required library with an alias −import pandas as pd;Create a DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'], "Place": ['Delhi', 'Bangalore', 'Hyderabad', 'Chandigarh', 'Pune', 'Mumbai', 'Jaipur'], "Units": [100, 150, 50, 110, 90, 120, 80] } ) Get the datatype and other info about the DataFrame −dataFrame.info()ExampleFollowing is the code −import pandas as pd; # create a DataFrame dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Audi', 'BMW', ... Read More

Python program to Convert Matrix to Dictionary Value List

AmitDiwan
Updated on 16-Sep-2021 08:05:43

457 Views

When it is required to convert a matrix to a dictionary value list, a simple dictionary comprehension can be used.ExampleBelow is a demonstration of the samemy_list = [[71, 26, 35], [65, 56, 37], [89, 96, 99]] print("The list is :") print(my_list) my_result = {my_index + 1 : my_list[my_index] for my_index in range(len(my_list))} print("The result is:") print(my_result)OutputThe list is : [[71, 26, 35], [65, 56, 37], [89, 96, 99]] The result is: {1: [71, 26, 35], 2: [65, 56, 37], 3: [89, 96, 99]}ExplanationA list of list is defined and is displayed on the console.A dictionary comprehension is ... Read More

Python program to randomly create N Lists of K size

AmitDiwan
Updated on 16-Sep-2021 08:00:28

275 Views

When it is required to create N lists randomly that are K in size, a method is defined that shuffles the values and yields the output.ExampleBelow is a demonstration of the samefrom random import shuffle def gen_random_list(my_val, K): while True: shuffle(my_val) yield my_val[:K] my_list = [12, 45, 76, 32, 45, 88, 99, 0, 1] print("The list is ") print(my_list) K, N = 4, 5 print("The value of K is ") print(K) print("The value of N is ") print(N) my_result = [] ... Read More

Python Pandas - Get unique values from a column

SaiKrishna Tavva
Updated on 23-Sep-2024 14:00:32

14K+ Views

There are several ways to extract unique values from a column in a data frame using Python Pandas, including unique() and nunique(). The panda's library in Python is mostly used for data analysis and manipulation to locate unique values in a data frame column. Some common methods to get unique values from a column are as follows: unique(): This method will return the unique values of a Series or DataFrame column as a NumPy array. ... Read More

Python - Count distinct in Pandas Aggregation with Numpy

AmitDiwan
Updated on 16-Sep-2021 07:46:45

780 Views

To count distinct, use nunique in Pandas. We will groupby a column and find sun as well using Numpy sum().At first, import the required libraries −import pandas as pd import numpy as npCreate a DataFrame with 3 columns. The columns have duplicate values −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Lexus'], "Place": ['Delhi', 'Bangalore', 'Delhi', 'Chandigarh', 'Chandigarh'], "Units": [100, 150, 50, 110, 90] } )Count distinct in aggregation agg() with nunique. Calculating the sum for counting, we are using numpy sum() −dataFrame = dataFrame.groupby("Car").agg({"Units": np.sum, "Place": pd.Series.nunique})ExampleFollowing is the code −import ... Read More

Python - Remove duplicate values from a Pandas DataFrame

AmitDiwan
Updated on 16-Sep-2021 07:28:05

779 Views

To remove duplicate values from a Pandas DataFrame, use the drop_duplicates() method. At first, create a DataFrame with 3 columns −dataFrame = pd.DataFrame({'Car': ['BMW', 'Mercedes', 'Lamborghini', 'BMW', 'Mercedes', 'Porsche'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Delhi', 'Hyderabad', 'Mumbai'], 'UnitsSold': [95, 70, 80, 95, 70, 90]})Remove duplicate values −dataFrame = dataFrame.drop_duplicates() ExampleFollowing is the complete code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame({'Car': ['BMW', 'Mercedes', 'Lamborghini', 'BMW', 'Mercedes', 'Porsche'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Delhi', 'Hyderabad', 'Mumbai'], 'UnitsSold': [95, 70, 80, 95, 70, 90]}) print"Dataframe...", dataFrame # counting frequency of column Car count = dataFrame['Car'].value_counts() print"Count in column ... Read More

Python – Group and calculate the sum of column values of a Pandas DataFrame

AmitDiwan
Updated on 16-Sep-2021 07:19:05

2K+ Views

We will consider an example of Car Sale Records and group month-wise to calculate the sum of Registration Price of car monthly. To sum, we use the sum() method.At first, let’s say the following is our Pandas DataFrame with three columns −dataFrame = pd.DataFrame(    {       "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"], "Date_of_Purchase": [ pd.Timestamp("2021-06-10"), pd.Timestamp("2021-07-11"), pd.Timestamp("2021-06-25"), ... Read More

What are the challenges faced by transport layer protocol?

Bhanu Priya
Updated on 16-Sep-2021 07:12:37

7K+ Views

In the OSI (Open System Interconnection) model, the transport layer is one of the seven layers and it is responsible for the end to end communication between the sender and receiver over the internet. It provides logical communication between the sender and receiver and ensures the end to end delivery of the packet.The transport layer main protocols are as follows −TCP (Transmission Control Protocol)UDP (User Datagram Protocol)SCTP (Stream Control Transmission Protocol)RDP (Reliable Data Protocol)RUDP (Reliable User Datagram Protocol)Responsibilities of the transport layerThe responsibilities of the transport layer are as follows −It provides a process to process delivery or end to ... Read More

Advertisements