我试图了解熊猫什么时候创建视图,什么时候在切片过程中复制
为了理解这种行为,我使用了以下方法:
# 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是错误的视图
总的趋势是什么
非常感谢您的建议和有用的链接
谨致问候
帕诺斯
数据帧的切片通常是一个视图,请参见以下链接
https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html
https://www.dataquest.io/blog/settingwithcopywarning/
希望这些帮助
相关问题 更多 >
编程相关推荐