My goal is to define a function that overwrites whatever input is given to it. It should add columns to the object and then merge it with a data frame defined within the function itself. I noticed that the columns I manually declare are being written on the object, but the columns that result from a merge are not being added.
This is what my data, df, looks like:
col1 col2
0 Q V V V V V V V V
1 Q V V V V V
2 Q V V V V V V V V
3 Q V V-- V V V V V V V
4 Q V V V V V V V V V V
In this dummy example, I would like to write a custom function that adds a column full of ones to the input and then merges it with another data frame. Note that the function is not returning another object, but rather, it is overwriting the object that was fed to it.
def f(data):
from pandas import DataFrame, merge
data['ones'] = 1
temp = DataFrame({'col1':['C','Q','M'], 'col3':[14,15,30]})
data = merge(data, temp, on='col1')
f(df)
col1 col2 ones
0 Q V V V V V V V V 1
1 Q V V V V V 1
2 Q V V V V V V V V 1
3 Q V V-- V V V V V V V 1
4 Q V V V V V V V V V V 1
Why is the result from the merge not being written over df while the df['ones'] is?