📅  最后修改于: 2023-12-03 15:18:15.731000             🧑  作者: Mango
The Pandas dropna()
function is used to remove missing or null values from a Pandas DataFrame or Series. It returns a new DataFrame or Series with missing values removed.
The syntax for using the dropna()
function is as follows:
DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
0
or 'index'
for rows and 1
or 'columns'
for columns.'any'
drops any row or column that has at least one missing value, while 'all'
drops a row or column if all its values are missing.True
, the function will modify the DataFrame or Series in place and return None
.Let's demonstrate how to use the dropna()
function with some examples:
import pandas as pd
# Create a sample DataFrame with missing values
df = pd.DataFrame({'A': [1, 2, None, 4],
'B': [5, None, None, 8],
'C': [None, None, None, None]})
# Remove rows with missing values
new_df = df.dropna()
print(new_df)
Output:
A B
0 1.0 5.0
import pandas as pd
# Create a sample DataFrame with missing values
df = pd.DataFrame({'A': [1, 2, 3, 4],
'B': [5, None, None, 8],
'C': [None, None, None, None]})
# Remove columns with missing values
new_df = df.dropna(axis=1)
print(new_df)
Output:
A
0 1
1 2
2 3
3 4
import pandas as pd
# Create a sample DataFrame with missing values
df = pd.DataFrame({'A': [1, 2, None, 4],
'B': [5, None, None, 8],
'C': [None, None, None, None]})
# Remove rows with at least 2 non-null values
new_df = df.dropna(thresh=2)
print(new_df)
Output:
A B
0 1.0 5.0
3 4.0 8.0
import pandas as pd
# Create a sample DataFrame with missing values
df = pd.DataFrame({'A': [1, 2, None, 4],
'B': [5, None, None, 8],
'C': [None, None, None, None]})
# Remove rows with missing values in the original DataFrame
df.dropna(inplace=True)
print(df)
Output:
A B
0 1.0 5.0
As demonstrated in these examples, the dropna()
function is a powerful tool for removing missing values from Pandas DataFrames and Series.