视图的绘制、切片和创建

2024-04-19 09:54:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图了解熊猫什么时候创建视图,什么时候在切片过程中复制

为了理解这种行为,我使用了以下方法:

# check view function
import pandas as pd
import numpy as np
# create a dataframe
df = pd.DataFrame(np.random.randn(10,5), index=list('abcdeqwrty'))
# take a slice
q = df[df[1]>0.5]
# check if it is a view (and it is)
print('q is a view {}'.format(q._is_view))
# change the view by keeping four columns, still a view
q = q.filter([0,1,2,3], axis='columns')
print('q is a view {}'.format(q._is_view))
# change the view by adding a column, not a view anymore
q['one more column'] = 'mplah'
print('q is a view {}'.format(q._is_view))

那个指纹

q是一种真实的观点
q是一种真实的观点
q是错误的视图

总的趋势是什么

非常感谢您的建议和有用的链接

谨致问候

帕诺斯


Tags: theimportview视图formatdfbyis