python Dataframe常用操作


DataFrame

讀取數據

  data=pd.read_table(filepath+ 'profile.txt',header=None)

  data.to_excel(filename)

  具體參考https://blog.csdn.net/u010801439/article/details/80052677

  隨即生成m*n的矩陣   df = pd.DataFrame(np.random.randn(m,n) , index = range(m),columns=range(n))

 

添加元素

  data=data.append(data.mean(),ignore_index=True )

  連接dataframe   datadeal= pd.concat([r2, r1], axis = 1) #橫向連接(0是縱向)

計算數據

  計算不同列的和  data['1-2'] = data.apply(lambda x: x[0]+x[1], axis=1)  

  計算所有行的和data.loc['Row_sum'] = data.apply(lambda x: x.sum())  #data.loc['Row_sum'] = data.apply(lambda x: x[0]+x[1])

選擇提取元素

  選擇不同的列 data.T[24:29].T ===data.iloc[:,24:29]

刪除元素

  rdata=data.drop([16,17]) 返回刪除刪掉16,17行的數據 ,axis=1則表示刪除列,加入參數inplace=True 則原始數據會改變,rdata為none

  del data['aa'] 刪除一列名為aa的元素

  time=data.pop('name')

 

數據分類:

data_group=data.groupby('INDEX')

def attr_trans(x):
    result=pd.Series(index=['NAME'])
    result['NAME']=x['NAME'].iloc[0]
    return result
data_process=data_group.apply(attr_trans)
print(data_process)    

圖像圖例右移: 

plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))

numpy數據翻轉 

    def VerticalFlipping(self,data):#垂直翻轉
        if data.shape[0]<=1:
            return data
        newarray=np.zeros(data.shape)
        for i in range(data.shape[0]):
            newarray[i]=data[-i-1]
        return newarray
    def HorizontalFlipping(self,data):#水平翻轉
        if data.shape[1]<=1:
            return data
        newarray=np.zeros(data.shape)
        for i in range(data.shape[1]):
            newarray[:,i]=data[:,-i-1]
        return newarray       

 locals()['data'+namearray[i]]=pd.read_table(filepath+ namearray[i]+'.txt',header=None)#動態創建變量

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM