在python中将文件写入txt给出typerror

时间:2018-09-12 14:10:26

标签: python pandas

import pandas as pd
with open('logAq.txt', 'w') as fw:
    a=pd.read_csv(r"C:\Users\user\Desktop\dddd.txt", sep=",")                   
    b=a[['dates','user']]
    b.groupby(['dates','user'])['dates'].count().sort_index(level=[0,1], ascending=[False,True])

    final=b.sort_values(['dates'],ascending=False).groupby(['dates','user'], sort=False)['dates'].count()
    fw.write(final)
  

TypeError:write()参数必须为str,而不是Series

final的结果写入txt应该怎么做?

2 个答案:

答案 0 :(得分:1)

如果变量final是列表,那么您只需要遍历值并将值写入输出中即可。

for value in final:
    fw.write(value+'\n')

或类似的东西...

答案 1 :(得分:0)

尝试在fw.write(final)之前添加str(final)