Python how to delete rows with empty columns

1. Summary

The dropna() method can find the null value (missing value) of DataFrame type data, delete the row/column where the null value is located, and return the new DataFrame as the return value.

2. Function explanation

Function form: dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)

parameter:

axis: axis. 0 or'index', means deleting by row; 1 or'columns', means deleting by column.

how: filter method. 'any' means that as long as the row/column has more than one null value, the row/column will be deleted;'all' means that the row/column is all null values, then the row/column will be deleted.

thresh: The minimum number of non-empty elements. int type, default is None. If the number of non-empty elements in the row/column is less than this value, the row/column is deleted.

subset: subset. List, the element is the index of the row or column. If axis=0 or'index', the element in subset is the index of the column; if axis=1 or'column', the element in the subset is the index of the row. The subarea restricted by subset is the conditional judgment area for judging whether to delete the row/column.

inplace: Whether to replace in place. Boolean value, the default is False. If it is True, the operation is performed on the original DataFrame and the return value is None.

3. Example

Create DataFrame data:

importnumpyasnp
importpandasaspd

a=np.ones((11,10))foriinrange(len(a)):
a[i,:i]=np.nan

d=pd.DataFrame(data=a)print(d)

Delete by row: if there is a null value, delete the row

# Delete by row: if there is a null value, delete the row
print(d.dropna(axis=0,how='any'))

Delete by row: all data is empty, that is, delete the row

# Delete by row: all data is empty, that is, delete the row
print(d.dropna(axis=0,how='all'))

Delete by column: If there are less than 5 non-empty elements in the column, delete the column

# Delete by column: If there are less than 5 non-empty elements in the column, delete the column
print(d.dropna(axis='columns',thresh=5))

Set a subset: delete rows where columns 0, 5, 6, and 7 are all empty

# Set a subset: delete rows where columns 0, 5, 6, and 7 are all empty
print(d.dropna(axis='index',how='all',subset=[0,5,6,7]))

Set a subset: delete columns with null values in rows 5, 6, and 7

# Set a subset: delete columns with null values in rows 5, 6, and 7
print(d.dropna(axis=1,how='any',subset=[5,6,7]))

In-place modification

# In-place modification
print(d.dropna(axis=0,how='any',inplace=True))print("==============================")print(d)

Example extension:

Code

import pandas as pd

data = pd.read_excel('test.xlsx',sheet_name='Sheet1')
datanota = data[data['salesperson'].notna()]print(datanota)

Output result

D:\Python\Anaconda\python.exe D:/Python/test/EASdeal/test.py
City sales amount salesperson
0 Beijing 10000 Zhang Lili
1 Shanghai 50000 Xiaoxiao
2 Shenzhen 60,000 benbenben
3 Chengdu 40000 Dada
Process finished with exit code 0

So far, this article on how to delete empty rows in python is introduced here. For more related python deletion methods of empty rows, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you all Support ZaLou.Cn a lot in the future!

Recommended Posts

Python how to delete rows with empty columns
How to make a globe with Python
How to delete cache files in python
How to process excel table with python
How to get started quickly with Python
How to convert web pages to PDF with Python
How to play happily with Python3 on Ubuntu
How to read and write files with Python
How to deal with python file reading failure
How to comment python code
How to learn python quickly
How to uninstall python plugin
How to understand python objects
How to use python tuples
python how to view webpage code
How to use python thread pool
How to write python configuration file
How to save the python program
How to omit parentheses in Python
How to install Python 3.8 on CentOS 8
How to write classes in python
How to filter numbers in python
Centos6.7 comes with python upgrade to
How to read Excel in Python
How to install Python on CentOS 8
How to view errors in python
How to write return in python
How to view the python module
How to understand variables in Python
How to clear variables in python
How to understand python object-oriented programming
How to use SQLite in Python
How to verify successful installation of python
How to use and and or in Python
How to represent null values in python
How to save text files in python
How to use PYTHON to crawl news articles
How to write win programs in python
How to run id function in python
How to custom catch errors in python
How to write try statement in python
How to delete redundant kernels in Ubuntu
How to delete redundant kernels in Ubuntu
How to define private attributes in Python
R&D: How To Install Python 3 on CentOS 7
How to add custom modules in Python
How to understand global variables in Python
How to view installed modules in python
How to install Python2 on Ubuntu20.04 ubuntu/focal64
How to debug python program using repr
How to learn the Python time module
How to open python in different systems
How to sort a dictionary in python
How to enter python triple quotation marks
How to add background music in python
How to represent relative path in python
How to monitor CentOS 7 server with Prometheus
How to install Prometheus with Docker on Ubuntu 14.04
How to upgrade all Python libraries on Ubuntu 18.04
How to use the round function in python
How to use the zip function in Python