Rewriting values ​​in the pandas framework based on NA values ​​from the second

If I have a pandas dataframe df1that is defined as:

df1 = pd.DataFrame({'a': [ 1, 2], 'b': [3, 4]})

And the dataframe is df2the same size, the same column names and index, but with NA values, somewhere, and

method A :

nan_locations = np.where(df2.isnull())
df1.values[nan_locations] = np.nan

method B :

df1[df2.isnull()] = np.nan

overwrites the values ​​in df1with NA from the same places in which they are in df2. However, if I do the same with a file frame read from an Excel file, method A does not always work. Maybe someone will explain to me why this is happening?

+4
source share
1 answer

dtypes DataFrame, . DataFrame , values ndarray dtype (doc), , -, . .

"" DataFrame, A B:

df1 = pd.DataFrame({'a': [1.0, 2.0], 'b': [3.0, 4.0]})
df2 = pd.DataFrame({'a': [1.0, 2.0], 'b': [np.nan, 4.0]})

, , , DataFrame B:

df1 = pd.DataFrame({'a': [1, 2], 'b': [3.0, 4.0]})
df2 = pd.DataFrame({'a': [1, 2], 'b': [np.nan, 4.0]})

dtypes df1:

a      int64
b    float64

, , Excel DataFrame dtypes. , .

+1

Source: https://habr.com/ru/post/1617325/


All Articles