关闭

pandas groupby重写Q3

278人阅读 评论(0) 收藏 举报
分类:

-- coding: utf-8 --

“””
Created on Thu Jul 09 20:31:38 2015

@author: Administrator
“”“

import pandas as pd
import numpy as np
import os

InputDir = r’D:\R\P’

rootdir = InputDir

pieces = []

for parent,dirnames,filenames in os.walk(rootdir):

 for filename in filenames:

    dayhourmin = filename.split('_')[4]
    day = dayhourmin[4:8]
    hour = dayhourmin[8:10]
    minute = dayhourmin[10:12]

    df=pd.read_csv(os.path.join(parent,filename),skiprows=3,header=None,nrows=8,sep=' ').iloc[:,2]
    #取第三列速度
    frame=df.T
    frame['day'] = day
    frame['hour'] = hour
    frame['minute'] = minute
    pieces.append(frame)
    wholeItem = pd.concat(pieces,axis = 1,ignore_index=True).replace('/////',np.nan).T.astype(np.float)
    print wholeItem.dtypes
    #注意元素类型

aver = wholeItem.groupby([‘day’,’hour’]).mean().add_prefix(‘mean_’)
all = pd.merge(wholeItem,aver,left_on=[‘day’,’hour’],right_index=True)

`

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26393次
    • 积分:649
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:6篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论