Programming Articles - Page 1108 of 3366

How to get the list of column headers from a Pandas DataFrame?

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 09:26:32

2K+ Views

To get a list of Pandas DataFrame column headers, we can use df.columns.values.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Print the list of df.columns.values output.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 "List of headers are: ", list(df.columns.values)OutputInput DataFrame is:    x  y  z 0  5  4  4 1  2  1  1 2  1  5  5 3  9 10  0 List of headers are: ['x', 'y', 'z']

How to get the row count of a Pandas DataFrame?

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

517 Views

To get the row count of a Pandas DataFrame, we can use the length of DataFrame index.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Print the length of the DataFrame index list, len(df.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 print "Row count of DataFrame is: ", len(df.index)OutputInput DataFrame is:    x  y  z 0  5  4  4 1  2  1  1 2  1  5  5 3  9 10  0 Row count of DataFrame is: 4

Select multiple columns in a Pandas DataFrame

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 09:20:35

2K+ Views

To select multiple columns in a Pandas DataFrame, we can create new a DataFrame from the existing DataFrameStepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Create a new DataFrame, df1, with selection of multiple columns.Print the new DataFrame with multiple selected columns.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 df1 = df[['x', 'y']] print "After selecting multiple columns:", df1OutputInput DataFrame is:    x  y  z 0  5  4  4 1  2  1  1 2  1  5  5 3  9 10  0 After selecting multiple columns:    x  y 0  5  4 1  2  1 2  1  5 3  9 10

How to rename column names in a Pandas DataFrame?

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

429 Views

To rename columns in a Pandas DataFrame, we can override df.columns with the new column names.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Override the columns with new list of column names.Print the DataFrame again with the renamed column names.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.columns = ["a", "b", "c"] print("After renaming, DataFrame is:", df)OutputInput DataFrame is:    x  y  z 0  5  4  4 1  2  1  1 2  1  5  5 3  9 10  0 After renaming, DataFrame is:    a  b  c 0  5  4  4 1  2  1  1 2  1  5  5 3  9 10  0

Select rows from a Pandas DataFrame based on column values

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 09:13:15

924 Views

To select rows from a DataFrame based on column values, we can take the following Steps −Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Use df.loc[df["x"]==2] to print the DataFrame when x==2.Similarly, print the DataFrame when (x >= 2) and (x < 2).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 "Given DataFrame is:", df print "When column x value == 2:", df.loc[df["x"] == 2] ... Read More

How to iterate over rows in a DataFrame in Pandas?

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 06:54:29

371 Views

To iterate rows in a DataFrame in Pandas, we can use the iterrows() method, which will iterate over DataFrame rows as (index, Series) pairs.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Iterate df using df.iterrows() method.Print each row with 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 "Given DataFrame:", df for index, row in df.iterrows():    print "Row ", index, "contains: "    print row["x"], row["y"], row["z"]OutputGiven DataFrame:    x   y   z 0  5   4   4 1  2   1   1 2  1   5   5 3  9  10   0 Row 0 contains: 5 4 4 Row 1 contains: 2 1 1 Row 2 contains: 1 5 5 Row 3 contains: 9 10 0

How to find the number of times array is rotated in the sorted array by recursion using C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 13:47:21

331 Views

Find index of mid element (minimum element) Apply Binary Search on the subarray based on following conditions −If number lies between start element and element at mid1 position.Then find number in array start to mid-1 using binary searchElse if number lies between mid and last element, then find number in array mid to last element using binary search.Example Live Demousing System; using System.Collections.Generic; using System.Text; using System.Linq; namespace ConsoleApplication{    public class Arrays{       public int FindNumberRotated(int[] array, int start, int end, int value){          if (start > end){             return ... Read More

How to find the minimum number of steps needed by knight to reach the destination using C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 13:40:34

357 Views

We have to make the knight cover all the cells of the board and it can move to a cell only once.There can be two ways of finishing the knight move - the first in which the knight is one knight's move away from the cell from where it began, so it can go to the position from where it started and form a loop, this is called closed tour, the second in which the knight finishes anywhere else, this is called open tour. A move is valid if it is inside the chessboard and if the cell is not ... Read More

How to find all the different combinations of opening and closing brackets from the given number k using C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 13:40:18

405 Views

Create a backtrack function that updates the current string if open brackets are less than n or close bracket are less than open bracket. When the length of current string becomes equal to 2*n, add it to the combination result array. It could be simply tracked by keeping the number of { } placed . We can start an opening bracket if we still have one left to place. And we can start a closing bracket if it would not exceed the number of opening brackets.Example Live Demousing System; using System.Collections.Generic; using System.Text; using System.Linq; namespace ConsoleApplication{    public class BackTracking{ ... Read More

How to find the unique combination of sum from the given number C#?

Nizamuddin Siddiqui
Updated on 27-Aug-2021 13:32:02

400 Views

Create an output list to store the valid sequences, create a current list that will store the current sequence found in the path of the recursion tree. A backtrack function that will go into the recursion until the target is achieved, otherwise, it should backtrack to the previous phase as target becomes less than 0. At any point in time, if target becomes 0 then add the candidate array to the result as the values in the candidate array must be sum up to the given target.If those are not the cases then, one by one add the elements in ... Read More

Advertisements