Suppose I have a dataframe like this, with a "dense" first column and a "sparse" second column:
# python 3.7.1, pandas 0.23.4.
import pandas as pd
df = pd.DataFrame({'col1':range(1,5), 'col2': [5, '', 7, '']})
missing_values_index = df[df['col2'] == ''].index
I tried two methods to assign col1 values to col2 missing values.
Method 1 (does not work, df remains unchanged):
df.loc[missing_values_index]['col2'] = df.loc[missing_values_index]['col1']
Method 2 (works ok):
df.loc[missing_values_index, 'col2'] = df.loc[missing_values_index, 'col1']
I thought these were just two ways of writing the same thing. Can someone explain what is really happening here?