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