📜  Pandas.dropna()(1)

📅  最后修改于: 2023-12-03 15:18:15.731000             🧑  作者: Mango

Pandas.dropna()

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)

Parameters:

  • axis: {0 or 'index', 1 or 'columns'}, default 0
    • Specifies the axis along which the function will drop missing values. 0 or 'index' for rows and 1 or 'columns' for columns.
  • how: {'any', 'all'}, default 'any'
    • Determines how the function will drop a row or column. '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.
  • thresh: int, optional
    • Specifies the minimum number of non-null values a row or column should have in order not to be dropped.
  • subset: array-like, optional
    • Specifies the list of column names to consider when dropping rows or columns with missing values.
  • inplace: bool, default False
    • If True, the function will modify the DataFrame or Series in place and return None.

Returns:

  • A new DataFrame or Series with missing values removed.

Examples:

Let's demonstrate how to use the dropna() function with some examples:

Example 1: Removing rows with missing values
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
Example 2: Removing columns with missing values
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
Example 3: Removing rows with a minimum number of non-null values
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
Example 4: Modifying the original DataFrame
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.