tima
tima

Reputation: 73

Unexpected transformation in pandas DataFrame while editing its copy

I have pandas DataFrame df with different types of columns, some values of df are NaN.

To test some assumption, I create copy of df, and transform copied df to (0, 1) with pandas.isnull():

df_copy = df
for column in df_copy:
    df_copy[column] = df_copy[column].isnull().astype(int)

but after that BOTH df and df_copy consist of 0 and 1. Why this code transforms df to 0, 1 and is there way to prevent it?

Upvotes: 0

Views: 59

Answers (2)

Quickbeam2k1
Quickbeam2k1

Reputation: 5437

You can prevent it declaring:

df_copy = df.copy()

This creates a new object. Prior to that you essentially had two pointers to the same object. You also might want to check this answer and note that DataFrames are mutable.

Btw, you could obtain the desired result simply by:

df_copy = df.isnull().astype(int)

Upvotes: 2

ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19395

even better memory-wise

for column in df:
    df[column + 'flag'] = df[column].isnull().astype(int)

Upvotes: 0

Related Questions