Make a copy of a dataframe
If you want to make a copy, you may use the Value Attribute of pandas.DataFrame and pandas.Series like
If you want to make a copy, you may use the Value Attribute of pandas.DataFrame and pandas.Series like
df = pd.DataFrame(data=[[1, 2, 3], [4, 5, 6]]) a = df.valuesNoto that it does not give a copy, gives just a view.
print(type(a)) print(np.shares_memory(df, a)) # <class 'numpy.ndarray'> # TrueUse .to_numpy() method to make a copy of a DataFrame or Series. But there is one thing to remain. Set the parameter of copy to be true. The default is false.
view = df.to_numpy() print(type(view)) print(np.shares_memory(df, view)) # <class 'numpy.ndarray'> # True
copy = df.to_numpy(copy=True) print(type(copy)) print(np.shares_memory(df, copy)) # <class 'numpy.ndarray'> # False
Comments
Post a Comment