-1

I am using drop in pandas with an inplace=True set. I am performing this on a duplicate dataframe, but the original dataframe is also being modified.

df1 = df
for col in df1.columns:
   if df1[col].sum() > 1:
      df1.drop(col,inplace=True,axis=1)

This is modifying my 'df' dataframe and don't seem to understand why.

mcsoini
  • 6,280
  • 2
  • 15
  • 38
user10939484
  • 167
  • 2
  • 13
  • You don't know a very important concept. Pandas `DataFame` is a **mutable** type object. For better understanding check this out: [https://medium.com/@meghamohan/mutable-and-immutable-side-of-python-c2145cf72747] – oo00oo00oo00 Nov 27 '19 at 21:04
  • I do, I usually use the copy function as stated in another answer. Just didn't use it here. – user10939484 Nov 27 '19 at 21:06
  • "I am performing this on a duplicate dataframe" Your code says otherwise. You simply have two names for the same dataframe. – MisterMiyagi Nov 27 '19 at 21:12
  • Does this answer your question? [Python Copy Through Assignment?](https://stackoverflow.com/questions/2438938/python-copy-through-assignment) – MisterMiyagi Nov 27 '19 at 21:13

1 Answers1

3

Use df1 = df.copy(). Otherwise they are the same object in memory.

However, it would be better to generate a new DataFrame directly, e.g.

df1 = df.loc[:, df.sum() <= 0]
mcsoini
  • 6,280
  • 2
  • 15
  • 38