Found 26504 Articles for Server Side Programming

How to check if any value is NaN in a Pandas DataFrame?

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 11:49:21

944 Views

To check if any value is NaN in a Pandas DataFrame, we can use isnull().values.any() method.StepsMake a series, s, one-dimensional ndarray with axis labels (including time series).Print the series, s.Check whether NaN is present or not.Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Check whether NaN is present or not.Example Live Demoimport pandas as pd import numpy as np s = pd.Series([1, np.nan, 3, np.nan, 3, np.nan, 7, np.nan, 3]) print "Input series is:", s present = s.isnull().values.any() print "NAN is present in series: ", present df = pd.DataFrame(    {       "x": [5, ... Read More

How to reset hierarchical index in Pandas?

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 11:47:12

352 Views

To reset hierarchical index in Pandas, we can use reset_index() method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Use groupby to get different levels of a hierarchical index and count it.Print multi-hierarchical index DataFrame.Reset the multi-hierarchical index DataFrame, using df.reset_index().Print the new updated DataFrame.Example Live Demoimport pandas as pd df = pd.DataFrame({"x": [5, 2, 1, 9], "y": [4, 1, 5, 10]}) print "Input DataFrame is:", df df1 = df.groupby(["x", "y"]).count() print "Hierarchical Index of input DataFrame is:", df1 df2 = df1.reset_index() print "After resetting: ", df2OutputInput DataFrame is:    x  y 0  5   ... Read More

Program to check if number of compass usage to get out of a maze is enough in Python

Arnab Chakraborty
Updated on 30-Aug-2021 12:46:14

119 Views

Suppose, we are playing a game where we are trapped in a maze. We have to find our way out of the maze. The maze can be presented as an x m matrix, where n is the number of rows and m is the number of columns. Each cell/element of the matrix contains any of the symbols 'O', 'D', 'S', or '-'. 'O' means that the path is blocked, 'D' is the way out from the maze, 'S' is our starting position, and '-' means we can move through the path. We can move freely through any of the '-' ... Read More

Program to find out the minimum number of moves for a chess piece to reach every position in Python

Arnab Chakraborty
Updated on 30-Aug-2021 11:37:28

612 Views

Suppose, we have a chessboard and a special knight piece K, that moves in an L shape within the board. If the piece is in position (x1, y1) and if it moves to (x2, y2) the movement can be described as x2 = x1 ± a ; y2 = y1 ± b or x2 = x1 ± b ; y2 = y1 ± a ; where a and b are integer numbers. We have to find out the minimum number of moves for that chess piece to reach to reach position (n-1, n-1) on the chessboard from the position (0, ... Read More

How to make a multi-index in Pandas?

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 10:11:46

326 Views

To make a multi-index in Pandas, we can use groupby with list of columns.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Print the index of DataFrame count.Use groupby to get different levels of a hierarchical index and count it.Print the mulitindex set in step 4.Example Live Demoimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 1, 9],       "y": [4, 1, 5, 10],       "z": [4, 1, 5, 0]    } ) print "Input DataFrame is:", df print "Default index: ", df.count().index df1 = df.groupby(["x", "y"]).count() ... Read More

Convert a Pandas DataFrame to a NumPy array

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 10:02:39

714 Views

To convert a Pandas DataFrame to a NumPy array, we can use to_numpy().StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Print the NumPy array of the given array, using df.to_numpy().Print the NumPy array of the given array for a specific column, using df['x'].to_numpy().Example Live Demoimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 1, 9],       "y": [4, 1, 5, 10],       "z": [4, 1, 5, 0]    } ) print "Input DataFrame is:", df print "DataFrame to numpy is:", df.to_numpy() print "DataFrame to numpy is:", df['x'].to_numpy()OutputInput ... Read More

How to count the NaN values in a column in a Python Pandas DataFrame?

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 09:57:44

2K+ Views

To count the NaN values in a column in a Pandas DataFrame, we can use the isna() method with sum.StepsCreate a series, s, one-dimensional ndarray with axis labels (including time series).Print the series, s.Count the number of NaN present in the series.Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Find NaN count column wise.Print the count DataFrame.Example Live Demoimport pandas as pd import numpy as np s = pd.Series([1, np.nan, 3, np.nan, 3, np.nan, 7, np.nan, 3]) print "Input series is:", s count = s.isna().sum() print "NAN count in series: ", count df = pd.DataFrame(   ... Read More

Deleting a DataFrame row in Python Pandas based on column value

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 09:45:30

3K+ Views

To delete a DataFrame row in Pandas based on column value, we can take the following Steps −StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Here, we will delete the row from the DataFrame that contains 0 in its Z-column, using df=df[df.z != 0]Print the updated DataFrame, after deleting row based on column value.Example Live Demoimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 1, 9],       "y": [4, 1, 5, 10],       "z": [4, 1, 5, 0]    } ) print "Input DataFrame is:", df df ... Read More

How are iloc and loc different in Python Pandas?

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 09:42:42

347 Views

Let's take an example to understand the difference between iloc and loc. Basically loc[0] returns the value present at 0 index, whereas iloc[0] returns the value present at the first location of a series.StepsCreate a one-dimensional ndarray with axis labels (including time series).Print the input series.Use loc[0] to print the value present at 0th index.Use iloc[0] to print the value present at the first location of the series table.Example Live Demoimport pandas as pd s = pd.Series(list("AEIOU"), index=[2, 1, 0, 5, 8]) print "Input series is:", s print "Value at index=0:", s.loc[0] print "Value at the 1st location of the series:", ... Read More

Writing a Pandas DataFrame to CSV file

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 09:39:48

4K+ Views

To write a Pandas DataFrame to CSV file, we can take the following Steps −StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Use df.to_csv to save the values of the DataFrame to a CSV (comma-separated values) file.Example Live Demoimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 1, 9],       "y": [4, 1, 5, 10],       "z": [4, 1, 5, 0]    } ) print "Input DataFrame is:", df df.to_csv("test.csv", sep='\t')OutputInput DataFrame is:    x   y  z 0  5  4  4 1  2  1  1 2  1  5  5 3  9 10  0It will create a new file ("test.csv") and save the values of the DataFrame in it.

Advertisements