Python & Pandas: How to return a copy of a dataframe?
<p>Here is the problem. I use a function to return a randomized data, </p>

<pre><code>data1 = [3,5,7,3,2,6,1,6,7,8]
data2 = [1,5,2,1,6,4,3,2,7,8]
df = pd.DataFrame(data1, columns = ['c1'])
df['c2'] = data2

def randomize_data(df):
    df['c1_ran'] = df['c1'].apply(lambda x: (x + np.random.uniform(0,1)))
    df['c1']=df['c1_ran']
    # df.drop(['c1_ran'], 1, inplace=True)
    return df

temp_df = randomize_data(df)

display(df)
display(temp_df)
</code></pre>

<p>However, the <code>df</code> (source data) and the <code>temp_df</code> (randomized_data) is the same. Here is the result:</p>

<p><a href="https://i.stack.imgur.com/TaBta.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TaBta.png" alt="enter image description here"></a></p>

<p>How can I make the <code>temp_df</code> and <code>df</code> different from each other?</p>

<hr>

<p>I find I can get rid of the problem by adding <code>df.copy()</code> at the beginning of the function </p>

<pre><code>def randomize_data(df):
    df = df.copy()
</code></pre>

<p>But I'm not sure if this is the right way to deal with it?</p>