Pandas 将列转换成行, 通过Groupby分组

for name, group in xfdps_all.groupby(['System_ID']):#首先对原始数据进行groupby
    # print name
    # print group
    newdf=pd.DataFrame({name:list(group['Service Call Close Date'])})#构建新的dataframe
    newdf[name]=pd.to_datetime(newdf[name])#转换数据格式为日期
    # print newdf
    newdf2=newdf.sort_values(by=name,ascending=True)#对时间进行排序
    print newdf2.shape
    print newdf2.T   #转置,由列变成行
    tempdf=tempdf.append(newdf2.T)
    print tempdf.shape
tempdf.to_excel("D:\\xfd-ps\\xfdps_1031.xlsx")#输出结果
```

你可能感兴趣的:(Pandas 将列转换成行, 通过Groupby分组)