Several cases of Pandas deleting data (summary)

Time:2019-8-12

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

1. Delete specific columns
2. Delete specific rows
3. Delete rows or columns that contain certain values
4. Delete rows or columns containing certain characters or words

In view of these four situations, this paper explores how to operate.

Data preparation

A record of stock delivery was simulated.

In [1]: import pandas as pd

In [2]: data = {
  ...:'Securities Name': ['Gree Electric','Visual China','Chengdu Bank','China Unicom','Gree Electric','Visual China','Chengdu Bank','China Unicom'],
  ['Securities Buy','Securities Buy','Securities Buy','Securities Buy','Securities Buy','Securities Sell','Securities Sell','Securities Sell'],
  ...:'Volume of Transactions': [500,1000,1500,2000,500,500,1000,1500],
  ...:'Transaction Amount': [-5000, -10000, -15000, -20000, 5500, 5500, 11000, 15000]
  ...: }
  ...: 

In [3]: df = pd.DataFrame(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]: 
     Summary of transaction volume and transaction amount of securities
2018-2-1 500-5000 Securities Buy Gree Electrical Appliances
2018-2-1,000-10,000 Securities Buy Visual China
2018-2-1 1500-15 000 Securities Buy Chengdu Bank
2018-2-1,2000-20,000 Securities Buying China Unicom
2018-2-2 500 5500 Securities Sell Gree Electrical Appliances
2008-2-2 500 5500 Securities Selling Visual China
2018-2-2 1000 11000 Securities Sell Bank of Chengdu
2018-2-3 1500 15000 Securities Sell China Unicom

Delete specific columns

In [5]: df. drop ('volume', axis = 1)
Out[5]: 
      Summary of Transaction Amount Securities Name
2018-2-1-5000 Securities Buy Gree Electric Appliances
2018-2-1-10000 Securities Buy Visual China
2018-2-1-15000 Securities Buy Chengdu Bank
2018-2-1-20000 Securities Buy China Unicom
2018-2-2 5500 Securities Sell Gree Electrical Appliances
2008-2-2 5500 Securities Selling Visual China
2018-2-2 11000 Securities Sell Bank of Chengdu
2018-2-3 15000 Securities Sell China Unicom

Delete specific rows

In [6]: df.drop('2018-2-3')
Out[6]: 
     Summary of transaction volume and transaction amount of securities
2018-2-1 500-5000 Securities Buy Gree Electrical Appliances
2018-2-1,000-10,000 Securities Buy Visual China
2018-2-1 1500-15 000 Securities Buy Chengdu Bank
2018-2-1,2000-20,000 Securities Buying China Unicom
2018-2-2 500 5500 Securities Sell Gree Electrical Appliances
2008-2-2 500 5500 Securities Selling Visual China
2018-2-2 1000 11000 Securities Sell Bank of Chengdu

You can also delete records by line number, such as deleting the third line.

In [22]: df.drop(df.index[7])
Out[22]: 
     Summary of transaction volume and transaction amount of securities
2018-2-1 500-5000 Securities Buy Gree Electrical Appliances
2018-2-1,000-10,000 Securities Buy Visual China
2018-2-1 1500-15 000 Securities Buy Chengdu Bank
2018-2-1,2000-20,000 Securities Buying China Unicom
2018-2-2 500 5500 Securities Sell Gree Electrical Appliances
2008-2-2 500 5500 Securities Selling Visual China
2018-2-2 1000 11000 Securities Sell Bank of Chengdu

Note that this method is not deleted by line number, but by index. If index is 3, the first four records will be deleted. This method supports a range and deletes it from the end in negative numbers.

Delete rows with specific values (Delete transactions less than 10,000)

In [7]: DF [DF ['transaction amount'] > 10000]
Out[7]: 
     Summary of transaction volume and transaction amount of securities
2018-2-2 1000 11000 Securities Sell Bank of Chengdu
2018-2-3 1500 15000 Securities Sell China Unicom

This example is actually filtering, if you need to retain, you can assign the filtered object to yourself.

Delete a column containing special characters

In [11]: DF [~df ['Securities Name']. str. contains ('Unicom')]
Out[11]: 
     Summary of transaction volume and transaction amount of securities
2018-2-1 500-5000 Securities Buy Gree Electrical Appliances
2018-2-1,000-10,000 Securities Buy Visual China
2018-2-1 1500-15 000 Securities Buy Chengdu Bank
2018-2-2 500 5500 Securities Sell Gree Electrical Appliances
2008-2-2 500 5500 Securities Selling Visual China
2018-2-2 1000 11000 Securities Sell Bank of Chengdu

If you want to retrieve records containing certain characters, you can remove them~

In [12]: DF [DF ['securities name']. str. contains ('Unicom')]
Out[12]: 
     Summary of transaction volume and transaction amount of securities
2018-2-1,2000-20,000 Securities Buying China Unicom
2018-2-3 1500 15000 Securities Sell China Unicom

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.

Recommended Today

Implementation of PHP Facades

Example <?php class RealRoute{ public function get(){ Echo’Get me’; } } class Facade{ public static $resolvedInstance; public static $app; public static function __callStatic($method,$args){ $instance = static::getFacadeRoot(); if(!$instance){ throw new RuntimeException(‘A facade root has not been set.’); } return $instance->$method(…$args); } // Get the Facade root object public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } protected […]