i was reading views vs copies in pandas(links : 1 2 3) and i was experimenting a dataframe after slicing and copying with _is_copy and _is_view attribute. But unable to understand it fully.
从文档中
每当涉及标签数组或布尔向量时 索引操作,结果将是副本。单标签/ 标量索引和切片,例如df.ix [3:6]或df.ix [:,'A'],视图 将被退回。
快照
码
# In[1] :
import pandas as pd
# In[2]:
data=pd.read_csv('titanic.csv')
data.head()
# In[4]:
#copying a column
new_age=data.age.copy()
# In[6]:
#checking wheather the copied data is view or a copy
new_age._is_view, new_age._is_copy is None
#Fine : it a copy, not view
# In[7]:
#Now doing another operation
new_data1=data.loc[data.age<10,:]
new_data2=data.loc[data.age<10,:].copy()
# In[8]:
# checking if it is a view
new_data1._is_view, new_data2._is_view
# Both are not view : thus a copy
# In[10]:
new_data1._is_copy is None, new_data2._is_copy is None
# as first results a false, new_data1 is copy
# while the second new_data2 is neither a copy nor view even if copy() is used
- 因此,当我尝试复制单个列时,_is_view和_is_copy正常工作。这是一个副本
- 确实。
- 当我尝试获取年龄小于1的元素的数据,即没有copy()方法的new_data1时,它是一个
- 复制,因为我在做布尔蒙版。因此,is_copy未设置为None。
问题
Q1。然后,当我使用copy()获取元素并分配给new_data2时,它既不是 复制或查看。
new_data2._is_view # False
new_data2._is_copy is None #True
那么new_data2是什么?复制或查看?
just use
df._is_view
and will return a boolean