Delete 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

Difference Between iloc and loc in Python Pandas

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

362 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

Write 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.

Select Rows from a Pandas DataFrame Using a List of Values

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

2K+ Views

To select the rows from a Pandas DataFrame based on input values, we can use the isin() method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame.Create a list of values for selection of rows.Print the selected rows with the given values.Next, print the rows that were not selected.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:", df values = [1, 2] print "Selected Rows:", ... Read More

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

328 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

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

Rename Column Names in a Pandas DataFrame

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

433 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

Advertisements