Found 10476 Articles for Python

Explain how a dataframe structure can be created using list of dictionary values in Python?

AmitDiwan
Updated on 10-Dec-2020 12:52:38

121 Views

Dataframe is a two dimensional data structure, where data is stored in a tabular format, in the form of rows and columns.It can be visualized as an SQL data table or an excel sheet representation.It can be created using the following constructor −pd.Dataframe(data, index, columns, dtype, copy)The ‘data’, ‘index’, ‘columns’, ‘dtype’ and ‘copy’ are not compulsory values.A list of dictionaries can be passed as input to the dataframe. The keys of dictionary are taken as column names by default. Let us see an example −Example Live Demoimport pandas as pd my_data = [{'ab' : 34}, {'mn' : 56}, { 'gh' : ... Read More

What happens if the specified index is not present in the series Python Pandas?

AmitDiwan
Updated on 10-Dec-2020 12:50:18

520 Views

When the index values are customized, they are accessed using series_name[‘index_value’]. The ‘index_value’ passed to series is tried to be matched to the original series. If it is found, that corresponding data is also displayed on the console.When the index that is tried to be accessed is not present in the series, it throws an error. It has been shown below.Example Live Demoimport pandas as pd my_data = [34, 56, 78, 90, 123, 45] my_index = ['ab', 'mn' ,'gh', 'kl', 'wq', 'az'] my_series = pd.Series(my_data, index = my_index) print("The series contains following elements") print(my_series) print("Accessing elements using customized index") print(my_series['mm'])OutputThe series ... Read More

How to retrieve multiple elements from a series when the index is customized Python?

AmitDiwan
Updated on 10-Dec-2020 12:48:51

1K+ Views

When the index values are customized, they are accessed using series_name[‘index_value’].The ‘index_value’ passed to series is tried to be matched to the original series. If it is found, that corresponding data is also displayed on the console.Let us see how multiple elements can be displayed.Example Live Demoimport pandas as pd my_data = [34, 56, 78, 90, 123, 45] my_index = ['ab', 'mn' ,'gh', 'kl', 'wq', 'az'] my_series = pd.Series(my_data, index = my_index) print("The series contains following elements") print(my_series) print("Accessing multiple elements using customized index") print(my_series[['mn', 'az', 'wq', 'ab']])OutputThe series contains following elements ab  34 mn  56 gh  78 kl  90 wq ... Read More

Explain how series data structure in Python can be created using scalar/constant values?

AmitDiwan
Updated on 10-Dec-2020 12:45:35

283 Views

Scalar or constant values are defined once, and they are repeated across all rows/entries of the series data structure. Following is an example −Example Live Demoimport pandas as pd my_index = ['ab', 'mn' ,'gh', 'kl'] my_series = pd.Series(7, index = my_index) print("This is series data structure created using scalar values and specifying index values") print(my_series)OutputThis is series data structure created using scalar values and specifying index values ab   7 mn   7 gh   7 kl   7 dtype: int64ExplanationThe required libraries are imported, and their alias are given so that it is easy to use them.A list of index ... Read More

How can Seaborn library be used to display a Scatter Plot in Python?

AmitDiwan
Updated on 10-Dec-2020 12:44:08

203 Views

Visualizing data is an important step since it helps understand what is going on in the data without actually looking at the numbers and performing complicated computations. Seaborn is a library that helps in visualizing data.Scatter plot shows the distribution of data as data points that are spread/scattered on the graph. It uses dots to represents values of a dataset, which are numeric in nature. The position of every dot on the horizontal and vertical axis denotes the value for a single data point.They help understand the relationship between two variables. Let us understand how this can be achieved using ... Read More

How can series be created using Numpy and passing index value explicitly in Python?

AmitDiwan
Updated on 10-Dec-2020 12:42:59

226 Views

Let us see how a series data structure can be created with the help of a Numpy array, and explicitly giving values for ‘index’.When no value is specified for index, default values beginning from 0 are assigned to values in the series.Following is an example −Example Live Demoimport pandas as pd import numpy as np my_data = np.array(['ab', 'bc', 'cd', 'de', 'ef', 'fg', 'gh', 'hi']) my_index = [3, 5, 7, 9, 11, 23, 45, 67] my_series = pd.Series(my_data, index = my_index) print("This is series data structure created using Numpy array and specifying index values") print(my_series)OutputThis is series data structure created using ... Read More

Program to check number of requests that will be processed with given conditions in python

Arnab Chakraborty
Updated on 03-Dec-2020 06:05:58

438 Views

Suppose we have a list of requests where each list contains elements like [uid, time_sec] (uid is the user id and time_sec is the timestamp). This indicates the user whose id is uid has requested to a website at timestamp time_sec. We also have two values u and g where u denotes maximum number of requests that are allowed in any < 60 second frame for a given uid and g is the maximum number of requests that are allowed in any < 60 second frame globally. Now if we want to process each request one by one and rate ... Read More

Program to check some elements in matrix forms a cycle or not in python

Arnab Chakraborty
Updated on 03-Dec-2020 06:04:32

216 Views

Suppose we have a 2d matrix. We have to check whether we can start from some cell then move adjacent cells (up, down, left, right) of the same value, and come back to the same starting point. We cannot revisit a cell that we have visited last.So, if the input is like222121212221then the output will be True, as we can follow 2s to form a cycle.To solve this, we will follow these steps −R := row count of matrixC := column count of matrixvis := make a matrix of size R x C and fill with FalseDefine a function dfs() ... Read More

Program to count how many ways we can cut the matrix into k pieces in python

Arnab Chakraborty
Updated on 03-Dec-2020 06:01:58

215 Views

Suppose we have a binary matrix and another value k. You want to split the matrix into k pieces such that each piece contains at least one 1 in it. But there are some rules for cutting, we have to follow in order: 1. Select a direction: vertical or horizontal 2. Select an index in the matrix to cut into two sections. 3. If we cut vertically, we can no longer cut the left part but can only continue cutting the right part.  4. If we cut horizontally, we can no longer cut the top part and can only continue ... Read More

Program to check whether palindrome can be formed after deleting at most k characters or not in python

Arnab Chakraborty
Updated on 03-Dec-2020 05:59:52

590 Views

Suppose we have a string s, we have to check whether we can make this string a palindrome after deleting at most k characters or not.So, if the input is like s = "lieuvrel", k = 4, then the output will be True, we can delete three characters to get palindrome "level".To solve this, we will follow these steps −Define a function lcs() . This will take a, bm := size of a, n := size of btable := matrix of size (m + 1) x (n + 1) and fill with 0for i in range 1 to m, dofor ... Read More

Advertisements