Create Pandas DataFrame by Appending One Row at a Time

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

4K+ Views

To create a Pandas DataFrame by appending one row at a time, we can iterate in a range and add multiple columns data in it.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Iterate in a range of 10.Assign values at different index with numbers.Print the created DataFrame.Example Live Demoimport pandas as pd import random df = pd.DataFrame(    {       "x": [],       "y": [],       "z": []    } ) print "Input DataFrame:", df for i in range(10):    df.loc[i] = [i, random.randint(1, 10), random.randint(1, 10)] print "After ... Read More

Change the Order of Pandas DataFrame Columns

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

339 Views

To change the order of DataFrame columns, we can take the following Steps −StepsMake two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Get the list of DataFrame columns, using df.columns.tolist()Change the order of DataFrame columns.Modify the order of columns of the DataFrame.Print the DataFrame after changing the columns order.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 cols = df.columns.tolist() cols = cols[-1:] + ... Read More

Get 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']

Get Row Count of a Pandas DataFrame

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

527 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

Rename Column Names in a Pandas DataFrame

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

468 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

944 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

What is Requirement Traceability Matrix

Vineet Nanda
Updated on 30-Aug-2021 07:05:32

2K+ Views

Introduction to Requirement Traceability MatrixA traceability matrix is a table-style document that is used to track requirements in the development of software applications. It can be used to trace backwards (from Coding to requirement) as well as forwards (from Requirements to Design or Coding). Requirement Traceability Matrix (RTM) or Cross Reference Matrix are other names for it (CRM).It is produced prior to the test execution process to ensure that all requirements are addressed in the form of a Test case, ensuring that no testing is missed. We connect all of the requirements to their associated test cases in the RTM ... Read More

Iterate Over Rows in a DataFrame in Pandas

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

383 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

Find Number of Times Array is Rotated in Sorted Array by Recursion

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

348 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

Advertisements