Check Compass Usage to Exit Maze in Python

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

140 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

Combine Two Series into a DataFrame in Pandas

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:23:44

2K+ Views

To combine two series into a DataFrame in Pandas, we can take two series and concatenate them using concat() method.StepsCreate series 1 with two elements, where index is ['a', 'b'] and name is Series 1.Print Series 1.Make Series 2 with two elements, where index is ['a', 'b'] and name is Series 2.Print Series 2.Concatenate Pandas objects along a particular axis with optional set logic along the other axes.Print the resultant DataFrame.Example Live Demoimport pandas as pd s1 = pd.Series([4, 16], index=['a', 'b'], name='Series 1') print "Input series 1 is: ", s1 s2 = pd.Series([3, 9], index=['a', 'b'], name='Series 2') print "Input series 2 is: ... Read More

Sort Multiple Columns of a Pandas DataFrame

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:21:38

988 Views

To sort multiple columns of a Pandas DataFrame, we can use the sort_values() method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Initialize a variable col to sort the column.Print the sorted DataFrame.Example Live Demoimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 7, 0],       "y": [4, 7, 5, 1],       "z": [9, 3, 5, 1]    } ) print "Input DataFrame is:", df col = ["x", "y"] df = df.sort_values(col, ascending=[False, True]) print "After sorting column ", col, "DataFrame is:", dfOutputInput DataFrame is:    x  y  z 0  5  4  9 1  2  7  3 2  7  5  5 3  0  1  1 After sorting column ['x', 'y'] DataFrame is:    x  y  z 2  7  5  5 0  5  4  9 1  2  7  3 3  0  1  1

Sort a Column of a Pandas DataFrame

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:20:01

457 Views

To sort a column in a Pandas DataFrame, we can use the sort_values() method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print input DataFrame, df.Initialize a variable col to sort the column.Print the sorted DataFrame.Example Live Demoimport pandas as pd df = pd.DataFrame(    {      "x": [5, 2, 7, 0],      "y": [4, 10, 5, 1],     `"z": [9, 3, 5, 1]   } ) print "Input DataFrame is:", df col = "x" df = df[col].sort_values(ascending=False) print "After sorting column ", col, "DataFrame is:", dfOutputInput DataFrame is:    x  y  z 0  5  4  9 1  2 10  3 2  7  5  5 3  0  1  1 After sorting column x DataFrame is: 2  7 0  5 1  2 3  0 Name: x, dtype: int64

Use the Apply Function for a Single Column in Pandas

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:15:24

16K+ Views

We can use apply() function on a column of a DataFrame with lambda expression.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print input DataFrame, df.Override column x with lambda x: x*2 expression using apply() method.Print the modified DataFrame.Example Live Demoimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 1, 5],       "y": [4, 10, 5, 10],       "z": [1, 1, 5, 1]    } ) print "Input DataFrame is:", df df['x'] = df['x'].apply(lambda x: x * 2) print "After applying multiplication of 2 DataFrame is:", dfOutputInput DataFrame is:    x  y  z 0  5  4  1 1  2 10  1 2  1  5  5 3  5 10  1 After applying multiplication of 2 DataFrame is:     x  y   z 0  10  4   1 1   4 10   1 2   2  5   5 3  10 10   1

Count Frequency of a Value in DataFrame Column in Pandas

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:13:18

4K+ Views

To count the frequency of a value in a DataFrame column in Pandas, we can use df.groupby(column name).size() method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Print frequency of column, x.Print frequency of column, y.Print frequency of column, z.Example Live Demoimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 1, 5],       "y": [4, 10, 5, 10],       "z": [1, 1, 5, 1]    } ) print "Input DataFrame is:", df col = "x" count = df.groupby('x').size() print "Frequency of values in column ", col, ... Read More

Check If a Column Exists in Pandas

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

9K+ Views

To check if a column exists in a Pandas DataFrame, we can take the following Steps −StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Initialize a col variable with column name.Create a user-defined function check() to check if a column exists in the DataFrame.Call check() method with valid column name.Call check() method with invalid column name.Example Live Demoimport pandas as pd def check(col):    if col in df:       print "Column", col, "exists in the DataFrame."    else:       print "Column", col, "does not exist in the DataFrame." df = pd.DataFrame( ... Read More

Replace NaN Values by Zeroes in a Pandas DataFrame Column

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:03:02

754 Views

To replace NaN values by zeroes or other values in a column of a Pandas DataFrame, we can use df.fillna() method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Use df.fillna(0) to replace NaN in DataFrame with value 0.Similarly use df.fillna(5) and df.fillna(7) to replace NaN in DataFrame with 5 and 7, respectively.Print the replaced NaN, DataFrame.Example Live Demoimport pandas as pd import numpy as np df = pd.DataFrame(    {       "x": [5, np.nan, 1, np.nan],       "y": [np.nan, 1, np.nan, 10],       "z": [np.nan, 1, np.nan, np.nan]    } ... Read More

Replace NaN Values by Zeroes in a Pandas Series

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 11:57:35

997 Views

To replace NaN values by zeroes or other values in a column of Pandas Series, we can use s.fillna() method.StepsCreate a one-dimensional ndarray with axis labels (including time series).Print the input series.Use s.fillna(0) to replace NaN in the series with value 0.Similarly, use s.fillna(5) and s.fillna(7) to replace NaN in series with values 5 and 7, respectively.Print the replaced NaN series.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 print "After replacing NaN with 0:", s.fillna(0) print "After replacing NaN with 5:", s.fillna(5) ... Read More

Create DataFrame with Customized Index Parameters in Pandas

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 11:51:23

2K+ Views

To create a DataFrame with some index, we can pass a list of values and assign them into index in DataFrame Class.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Put a list of indices in the index of DataFrame class.Print the DataFrame with the customized index.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 = pd.DataFrame(    {       "x": [5, 2, 1, 9],       "y": [4, 1, 5, 10],       "z": [4, 1, 5, 0]    },    index=["John", "Jacob", "Ally", "Simon"] ) print "With Customized Index: ", dfOutputInput DataFrame is:    x  y  z 0  5  4  4 1  2  1  1 2  1  5  5 3  9  10 0 With Customized Index:        x  y   z John   5  4   4 Jacob  2  1   1 Ally   1  5   5 Simon  9  10  0

Advertisements