Instead of:
df2 = df2.insert(1, 'file_id_value', range(0, len(df2)))
Do:
df2.insert(1, 'file_id_value', range(0, len(df2)))
Explanation: in Python, everything is an object. Objects are created and saved into memory. Compared to other programming languages that do variable assignment by call-by-value, Python lets you externally modify an object without the need of saving the changes into the/a new variable (as long as the object is mutable type). For that reason you can operate into df2
without needing to make the insert
function return a new instance of df2
. This does not always apply; for instance, string are non-mutable, so they need re-asignment.
To learn more, you may want to check out this SO question: How are Python in-place operator functions different than the standard operator functions?
Quote for mutable objects relating the addition operator:
An object's iadd method also takes two parameters, but makes the change in-place, modifying the contents of the first parameter. Because this requires object mutation, immutable types (like the standard number types) shouldn't have an iadd method.