1

With regular Python lists, I can do the following:

x = [1, 2, 3]
y = x
x[:] = [4, 5, 6]
print(y)  # prints '[4, 5, 6]'

That is, I can replace the content of a list object (x) in-place with completely different values without loosing previously created references to it (y).

I'd like to do the same with a Pandas DataFrame. The following works, but only if the replacement has the same shape and columns as the original:

x = pd.DataFrame({'a': [1,2], 'b': [3,4]})   
y = x
x[:] = pd.DataFrame({'a': [5, 6], 'b': [7, 8]})
print(x)
print(y)

If the replacement has different column names then they are silently mapped to the original ones:

x[:] = pd.DataFrame({'c': [9, 10], 'd': [11, 12]})
print(x)

And if the shape of the replacement differs then a ValueError is raised:

x[:] = pd.DataFrame({
    'a': [13, 14, 15],
    'b': [16, 17, 18],
    'e': [18, 19, 20],
})
Florian Brucker
  • 9,621
  • 3
  • 48
  • 81

0 Answers0