data.dropna
时间: 2023-09-01 12:11:14 浏览: 46
data.zip
()This method drops all rows that contain any missing values (NaNs). It returns a new DataFrame with the same columns as the original, but with rows containing NaNs removed. If the original DataFrame has no NaNs, it returns a copy of the original DataFrame.
Example:
```python
import pandas as pd
# create a DataFrame with missing values
data = pd.DataFrame({'A': [1, 2, None, 4], 'B': [5, None, 7, 8], 'C': [9, 10, 11, None]})
print(data)
# drop all rows with missing values
clean_data = data.dropna()
print(clean_data)
```
Output:
```
A B C
0 1.0 5.0 9.0
1 2.0 NaN 10.0
2 NaN 7.0 11.0
3 4.0 8.0 NaN
A B C
0 1.0 5.0 9.0
```
In this example, the `dropna()` method removes the second and fourth rows because they contain NaNs, and returns a new DataFrame with only the first row.
阅读全文