Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

1,016 Articles Published

Articles by Rishikesh Kumar Rishi

Page 19 of 102

How to use the apply() function for a single column in Pandas?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 30-Aug-2021 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

Read More

Count the frequency of a value in a DataFrame column in Pandas

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 30-Aug-2021 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

How to check if a column exists in Pandas?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 30-Aug-2021 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

Create a DataFrame with customized index parameters in Pandas

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 30-Aug-2021 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

Read More

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

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 30-Aug-2021 1K+ 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
Rishikesh Kumar Rishi
Updated on 30-Aug-2021 386 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

How to make a multi-index in Pandas?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 30-Aug-2021 357 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
Rishikesh Kumar Rishi
Updated on 30-Aug-2021 772 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
Rishikesh Kumar Rishi
Updated on 30-Aug-2021 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
Rishikesh Kumar Rishi
Updated on 30-Aug-2021 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
Showing 181–190 of 1,016 articles
« Prev 1 17 18 19 20 21 102 Next »
Advertisements