SoFunction
Updated on 2025-03-01

Several situations of Pandas deleting data (summary)

Before starting, there may be several situations in which DataFrame deletes objects in pandas

1. Delete specific columns
2. Delete specific lines
3. Delete rows or columns containing certain values
4. Delete rows or columns containing certain characters or texts

This article discusses how to operate these four situations.

Data preparation

Simulated a record of stock delivery.

In [1]: import pandas as pd

In [2]: data = {
  ...:   'Securities Name' : ['Gree Electric Appliances','Visual China','Chengdu Bank','China Unicom','Gree Electric Appliances','Visual China','Chengdu Bank','China Unicom'],
  ...:   'summary': ['Securities Buy','Securities Buy','Securities Buy','Securities Buy','Selling securities','Selling securities','Selling securities','Selling securities'],
  ...:   'Trade quantity' : [500,1000,1500,2000,500,500,1000,1500],
  ...:   'Transaction amount' : [-5000,-10000,-15000,-20000,5500,5500,11000,15000]
  ...: }
  ...: 

In [3]: df = (data, index = ['2018-2-1','2018-2-1','2018-2-1','2018-2-1','2018-2-2','2018-2-2','2018-2-2','2018-2-3'])

In [4]: df
Out[4]: 
     Transaction quantity  Transaction amount  summary Securities name
2018-2-1  500 -5000 Securities buying Gree Electric Appliances
2018-2-1 1000 -10000 Securities buying Visual China
2018-2-1 1500 -15000 Securities buying Chengdu Bank
2018-2-1 2000 -20000 Securities buying China Unicom
2018-2-2  500  5500 Securities Sell Gree Electric Appliances
2018-2-2  500  5500 Securities Sell Visual China
2018-2-2 1000 11000 Securities Sell Chengdu Bank
2018-2-3 1500 15000 Securities Sell China Unicom

Delete specific columns

In [5]: ('Trade quantity',axis=1)
Out[5]: 
      Transaction amount  summary Securities name
2018-2-1 -5000 Securities buying Gree Electric Appliances
2018-2-1 -10000 Securities buying Visual China
2018-2-1 -15000 Securities buying Chengdu Bank
2018-2-1 -20000 Securities buying China Unicom
2018-2-2  5500 Securities Sell Gree Electric Appliances
2018-2-2  5500 Securities Sell Visual China
2018-2-2 11000 Securities Sell Chengdu Bank
2018-2-3 15000 Securities Sell China Unicom

Delete specific lines

In [6]: ('2018-2-3')
Out[6]: 
     Transaction quantity  Transaction amount  summary Securities name
2018-2-1  500 -5000 Securities buying Gree Electric Appliances
2018-2-1 1000 -10000 Securities buying Visual China
2018-2-1 1500 -15000 Securities buying Chengdu Bank
2018-2-1 2000 -20000 Securities buying China Unicom
2018-2-2  500  5500 Securities Sell Gree Electric Appliances
2018-2-2  500  5500 Securities Sell Visual China
2018-2-2 1000 11000 Securities Sell Chengdu Bank

You can also delete the record according to the line number, such as deleting the third line

In [22]: ([7])
Out[22]: 
     Transaction quantity  Transaction amount  summary Securities name
2018-2-1  500 -5000 Securities buying Gree Electric Appliances
2018-2-1 1000 -10000 Securities buying Visual China
2018-2-1 1500 -15000 Securities buying Chengdu Bank
2018-2-1 2000 -20000 Securities buying China Unicom
2018-2-2  500  5500 Securities Sell Gree Electric Appliances
2018-2-2  500  5500 Securities Sell Visual China
2018-2-2 1000 11000 Securities Sell Chengdu Bank

Note that this method is not actually deleted according to line number, but according to index. If the index is 3, the first 4 records will be deleted. This method supports a range and uses negative numbers to deletion from the end.

Delete rows with a specific value (delete transaction amount is less than 10,000)

In [7]: df[ df['Transaction amount'] > 10000]
Out[7]: 
     Transaction quantity  Transaction amount  summary Securities name
2018-2-2 1000 11000 Securities Sell Chengdu Bank
2018-2-3 1500 15000 Securities Sell China Unicom

This example is actually filtering. If you need to keep it, you can assign the filtered object to yourself.

Delete a row containing special characters in a column

In [11]: df[ ~ df['Securities Name'].('United Nations') ]
Out[11]: 
     Transaction quantity  Transaction amount  summary Securities name
2018-2-1  500 -5000 Securities buying Gree Electric Appliances
2018-2-1 1000 -10000 Securities buying Visual China
2018-2-1 1500 -15000 Securities buying Chengdu Bank
2018-2-2  500  5500 Securities Sell Gree Electric Appliances
2018-2-2  500  5500 Securities Sell Visual China
2018-2-2 1000 11000 Securities Sell Chengdu Bank

If you want to get a record containing certain characters, you can remove it.

In [12]: df[ df['Securities Name'].('United Nations') ]
Out[12]: 
     Transaction quantity  Transaction amount  summary Securities name
2018-2-1 2000 -20000 Securities buying China Unicom
2018-2-3 1500 15000 Securities Sell China Unicom

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.