How To Remove Negative Values From Dataframe In Python

In this tutorial, you will learn how to remove negative values from a dataframe in Python. This process can be helpful when cleaning and preparing data for analysis, as negative values might be incorrect or incompatible with the dataset’s purpose.

The primary library we’ll use for this task is Pandas, a powerful data manipulation library for Python.

Step 1: Installing Pandas

First, ensure that you have Pandas installed if you haven’t already. You can install it using pip:

Step 2: Importing Pandas

Now that Pandas is installed, import it into your Python script:

Step 3: Create a Dataframe

Let’s create a dataframe with some sample data that contains some negative values:

Our dataframe will look like this:

   A   B   C
0  1  -6  11
1 -2  -7  12
2  3   8 -13
3 -4   9 -14
4  5 -10  15

Step 4: Removing Negative Values

Now we’ll remove the negative values from the dataframe using the applymap() function along with a custom function that filters negative values:

The custom function remove_negative checks if a value is less than zero and replaces it with None if it is. The applymap() function applies this custom function to every element in the dataframe.

After applying this function, our dataframe will look like this:

     A    B     C
0  1.0  NaN  11.0
1  NaN  NaN  12.0
2  3.0  8.0   NaN
3  NaN  9.0   NaN
4  5.0  NaN  15.0

Step 5: Replacing NaN with Desired Value

You can replace the NaN values (which indicate that a value is “Not a Number”) with a desired value, such as 0 or any other valid number. Use the fillna() function to do this:

The dataframe now looks like this:

     A    B     C
0  1.0  0.0  11.0
1  0.0  0.0  12.0
2  3.0  8.0   0.0
3  0.0  9.0   0.0
4  5.0  0.0  15.0

Full Code

Here’s the complete code:

Conclusion

In this tutorial, you have learned how to remove negative values from a dataframe in Python by using applymap() and a custom function with Pandas library. You also learned how to replace NaN values with desired values using the fillna() function. This process is helpful when cleaning and preparing data for analysis to ensure compatibility and accuracy in the data set.