熊猫,切片和创建视图

时间:2020-01-02 21:00:53

标签: python pandas

我试图了解大熊猫何时创建视图以及切片时的副本。

要了解其行为,我使用了以下内容:

# 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是True视图
q是一个视图True
q是一个视图False

总体趋势是什么?

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

此致

Panos

1 个答案:

答案 0 :(得分:0)

数据框的一部分通常是视图,请参见以下链接

https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html

https://www.dataquest.io/blog/settingwithcopywarning/

希望这些帮助。