Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

DataFrame that is a partial view

Is it possible to create a dataframe where one fragment is a view of another df and the remaining fragment is not a view?

I am unable to create such a df, but I want to know if it is possible.

If this is possible, can you give an example of such a dataframe?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think you can make something like this when you construct the dataframe with copy=False parameter. Consider this:

arr = np.array([[1, 2, 3], [4, 5, 6]])

df1 = pd.DataFrame(arr, columns=["a1", "b1", "c1"], copy=False)

df2 = pd.DataFrame(arr, columns=["a2", "b2", "c2"], copy=False)
df2["d"] = 999

print(df1)
print(df2)

This prints:

   a1  b1  c1
0   1   2   3
1   4   5   6

   a2  b2  c2    d
0   1   2   3  999
1   4   5   6  999

Now when you do:

df1.loc[0, :] = -1

print(df1)
print(df2)

This prints:

   a1  b1  c1
0  -1  -1  -1
1   4   5   6

   a2  b2  c2    d
0  -1  -1  -1  999
1   4   5   6  999
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading