|
1 |
| -This doesn't work in pandas. Instead, the :func:`pd.isna` or :func:`pd.notna` functions |
2 |
| -should be used for comparisons. |
| 1 | +In pandas, :meth:`Series.isna` and :meth:`Series.notna` can be used to filter the rows. |
3 | 2 |
|
4 | 3 | .. ipython:: python
|
5 | 4 |
|
6 |
| - outer_join[pd.isna(outer_join["value_x"])] |
7 |
| - outer_join[pd.notna(outer_join["value_x"])] |
| 5 | + outer_join[outer_join["value_x"].isna()] |
| 6 | + outer_join[outer_join["value_x"].notna()] |
8 | 7 |
|
9 |
| -pandas also provides a variety of methods to work with missing data -- some of |
10 |
| -which would be challenging to express in Stata. For example, there are methods to |
11 |
| -drop all rows with any missing values, replacing missing values with a specified |
12 |
| -value, like the mean, or forward filling from previous rows. See the |
13 |
| -:ref:`missing data documentation<missing_data>` for more. |
| 8 | +pandas provides :ref:`a variety of methods to work with missing data <missing_data>`. Here are some examples: |
| 9 | + |
| 10 | +Drop rows with missing values |
| 11 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
14 | 12 |
|
15 | 13 | .. ipython:: python
|
16 | 14 |
|
17 |
| - # Drop rows with any missing value |
18 | 15 | outer_join.dropna()
|
19 | 16 |
|
20 |
| - # Fill forwards |
| 17 | +Forward fill from previous rows |
| 18 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 | + |
| 20 | +.. ipython:: python |
| 21 | +
|
21 | 22 | outer_join.fillna(method="ffill")
|
22 | 23 |
|
23 |
| - # Impute missing values with the mean |
| 24 | +Replace missing values with a specified value |
| 25 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 26 | + |
| 27 | +Using the mean: |
| 28 | + |
| 29 | +.. ipython:: python |
| 30 | +
|
24 | 31 | outer_join["value_x"].fillna(outer_join["value_x"].mean())
|
0 commit comments