<jupyter_start><jupyter_text>Create a sample DataFrame with some missing values.<jupyter_code>df = pd.DataFrame({
    'colA': ['aaa', NaN, NaN, NaN, 'bbb', 'ccc'],
    'colB': ['xxx', 'yyy', NaN, 'zzz', NaN, 'www'], 
    #'colC': [NaN, 3, NaN, 1, 0, 9]
    })
df<jupyter_output><empty_output><jupyter_text>**Task:** replace missing values in column `colA` with those in `colB` (if they exist). First we define a filtering expression ("condition") `cond` which encodes the condition which we'd like to use for filling in the values. In this case we could actually use the simpler condition `cond = df.colA.isnull()` because it doesn't matter if the value in `colB` is also missing (since we would just replace `NaN` with `NaN`), but for the sake of illustration let's use this slightly more complicated expression.<jupyter_code>cond = df.colA.isnull() & ~df.colB.isnull()
cond<jupyter_output><empty_output><jupyter_text>We can use this to extract the desired columns if we wish.<jupyter_code>df[cond]<jupyter_output><empty_output><jupyter_text>Now we can do the assignment. Note that we use the `.loc` operator to avoid a warning about "trying to set values on a copy of a slice from a DataFrame" which would happen if we used for example the following expression```df[cond]['colA'] = df[cond]['colB']```(See http://pandas.pydata.org/pandas-docs/stable/indexing.htmlindexing-view-versus-copy for details.)<jupyter_code>df.loc[cond, 'colA'] = df.loc[cond, 'colB']<jupyter_output><empty_output><jupyter_text>The resulting DataFrame does indeed have the values `yyy` and `zzz` filled in column `colA`.<jupyter_code>df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1278149726110773, 'fraction_numerical': 0.002434570906877663, 'mean_word_length': 5.821576763485477, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '267553', 'license': 'Unlicense', 'n_tokens_mistral': 558, 'n_tokens_neox': 533, 'n_words': 187, 'path': 'pandas-snippets.ipynb', 'repo_name': 'maxalbert/pandas-cookbook', 'size': '1643'}