import pandas as pd
data =[10,11,12]
index=['a','b','c']
s=pd.Series(data=data,index=index)
s
s[0]
s[0:2]
mask=[True,False,True]
s[mask]
s.loc['b']
s.iloc[1]
s1=s.copy()
s1['a']=100
s1
s1.replace(to_replace=100,value=101,inplace=True)#inplace=True時s1原來的值會變,反之爲False則不會變
s1
s1.index
s1.index=['a','b','d']#修改index的值
s1
s1.rename(index={'a':'A'},inplace=True)#inplace=True能夠改變原來s1的index值
s1
data=[11,110]
index=['h','k']
s2=pd.Series(data=data,index=index)
s2
s3=s1.append(s2)#增長一整個Series
s3['j']=500#單獨增長一個
s3
s1.append(s2,ignore_index=False)#ignore_index=False:不對序列進行從新排序,保留原來的排序
s1.append(s2,ignore_index=True)#ignore_index=True:對原來的序列進行從新排序
s3
del s3['A']#指定刪除1行
s3
s3.drop(['b','d'],inplace=True)#多行刪除
s3
data=[[1,2,3],[4,5,6]]
index=['a','b']
columns=['A','B','C']
df=pd.DataFrame(data=data,index=index,columns=columns)
df
df['A']
df.loc['a']
df.iloc[1]
df.loc['a']['A']
df.loc['a']['A']=150#直接在原數據上修改值
df
df.index=['f','g']#改索引
df
df.rename(index={'f':'F'},inplace=True)#改索引
df
df.loc['c']=[1,2,3]#增長1行
df
data=[[1,2,3],[4,5,6]]
index=['j','k']
columns=['A','B','C']
df2=pd.DataFrame(data=data,index=index,columns=columns)
df2
df3=pd.concat([df,df2],axis=0)#把兩個DataFrame鏈接
df3
df2['Tang']=[10,11]#增長一列
df2
df4=pd.DataFrame([[10,11],[12,13]],index=['j','k'],columns=['D','E'])
df4
df5=pd.concat([df2,df4],axis=1)
df5
df5.drop(['j'],axis=0,inplace=True)
df5
del df5['Tang']#刪1列
df5
df5.drop(['A','B','C'],axis=1,inplace=True)
df5