Pandas的View和Copy

Pandas的View和Copy

对frame或者series进行操作经常不知道会不会对原数据操作改变,而且与遇到SetingWithView的警告,做了下实验。

Series

太长不看:

  1. slice是view (和numpy保持一致)
  2. loc进行boolean筛选后的新Series是copy
  3. copy()方法后的是copy
  4. Series.T 转置之后的Series也是view (和numpy保持一致)

DataFrame

太长不看:

  1. 对index或者column 进行slice是view
  2. loc单选一个index 或一个column是view
  3. loc进行boolean筛选是copy
  4. loc进行[ ] or () 选择是copy
  5. 使用1.2.3.4对frame同时进行index和column的操作,当且仅当index和column都是结果为view操作时,返回的frame或series才是view,其他是copy

详细

太忙了,下次再写

测试code

import numpy as np
import pandas as pd

a=pd.Series([1,2,3,4,5,6,7,8,9])
aid(a.values),aid(a.copy().values),aid(a[0:2].values),aid(a.loc[a%2==0].values),aid(a.T.values)

A=pd.DataFrame([[0,0,0],[0,0,0],[0,0,0]],columns=["c1","c2","c3"])
B=A["c1"]
C=A.loc[:,"c1"]
C1=A.loc[:,["c1"]]
# D=A.loc[:,"c1":"c2"]
E=A.T
aid(A["c1"].values),aid(B.values),aid(C.values),aid(C1["c1"].values),aid(E.loc[].values) #,aid(D["c1"].values)

# another testing
A=pd.DataFrame([[0,0,0],[0,0,0],[0,0,0]],columns=["c1","c2","c3"])


B=A.loc[0]
C=A.loc[0:1]
C1=A.loc[0:1,["c1","c2"]]
C2=A.loc[0:1,"c1"]
D=A.loc[A.index<2]


A.at[0,"c1"]=1

print("A......")
print(A)
print("B......")
print(B)
print("C......")
print(C)
print("C1......")
print(C1)
print("C2......")
print(C2)
print("D......")
print(D)