out=groupby_sum.ix[:'to_uid','sum(diamonds)']
使用ix在提取数据的时候,out的数据类型通常为<class 'pandas.core.series.Series'>,即为Series类型。
但是Series类型没有直接的to_excel方法(out.to_excel('data2.xlsx','Sheet1')),所以是不能直接写入到文件中的,
解决办法:
将Series转化为DataFrame,然后再写入问价中即可。
Series.
to_frame
(name=None)
注意事项:在pandas官方文档的API Reference下有大量的类似知识,需要好好的研究。
#http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.to_frame.html
下面是一个demo:
# -*- coding: utf-8 -*-
# Time : 2016/11/29 11:17
# Author : XiaoDeng
# version : python3.5
# Software: PyCharm Community Edition
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#装载关系网UID
uid_dict={}
uid_list=[]
for k in open('关系网data.txt'):
k=k.strip()
k=k.split('\t')
uid=k[0]#即为需要查询数据的某ID
to_uid=k[1] #接收礼物的id
uid_list.append(int(to_uid)) #注意:记得转化为int类型
if uid not in uid_dict:
uid_dict[uid]=[to_uid]
else:
uid_dict[uid].append(to_uid)
# print(uid_dict)
df=pd.read_csv('201611.csv')
# print(df)
intday=df['intday']
send_uid=df['send_uid']#送礼的人
to_uid=df['to_uid']#接收礼物的人
gid=df['gid']#礼物编号
sum_diamonds=df['sum(diamonds)'] #对应编号礼物的钻石数合计
#过滤
# print(uid_list)
guolv=df['to_uid'].isin(uid_list)
s=df[guolv]
# print(s)
# s.to_excel('data2.xlsx','Sheet1')
groupby_sum=s.groupby('to_uid').sum()
# print(groupby_sum)
#写入文件
out=groupby_sum.ix[:'to_uid','sum(diamonds)']
print(type(out))
out=out.to_frame()
# print(out)
# out.to_excel('data2.xlsx','Sheet1',index=False)#不要索引
out.to_excel('data2.xlsx','Sheet1')
if __name__=='__main__':
pass