打赏

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)#动态创建变量

 

posted @   skycandy  阅读(10207)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示