1

Could you please let me know why the new column c is added to the original dataframe i.e., df_old?

df_old = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df_new=df_old
df_new['c']=[5,6]
print('Old:')
print(df_old)
print('New:')
print(df_new)

Output:

Old:
   a  b  c
0  1  3  5
1  2  4  6
New:
   a  b  c
0  1  3  5
1  2  4  6

In fact, I need to preserve the original dataframe as it was:

   a  b  
0  1  3  
1  2  4  

Thanks in advance,

ebrahimi
  • 912
  • 2
  • 13
  • 32

1 Answers1

5

Use assign which creates a copy of the old dataframe:

df_old = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df_new = df_old.assign(c=[5,6])
print(df_old)
print(df_new)

Output:

   a  b
0  1  3
1  2  4
   a  b  c
0  1  3  5
1  2  4  6
Scott Boston
  • 147,308
  • 15
  • 139
  • 187