2016-11-12 89 views
0

我想對此Dataframe進行分類(名爲:loandata),並將每個子數據框導入許多csv文件。第一步,我嘗試轉換的結果之一,但不幸失敗了,得到這個錯誤:TypeError:不能將'模塊'對象隱式轉換爲str

import pandas as pd 
import csv 
import os 

#readfile 
loandata=pd.DataFrame(pd.read_table('/Users/lixuefei/Desktop/Sample Dataset/test.txt',header = None,index_col=2)) 

#classify 
volume_type=list(set(loandata[3])) 
system_type=list(set(loandata[4])) 
area_name=list(set(loandata[5])) 

df=pd.DataFrame(loandata[(loandata[3]==volume_type[0])& (loandata[4]==system_type[0])&(loandata[5]==area_name[0])]) 
#set the file path 
path='/Users/lixuefei/Desktop/Sample Dataset' 
filename=volume_type[0]+system_type[0]+area_name[0] 
filetype=csv 

if not df.empty: 
    df.to_csv(os.path.join(path,filename+filetype),header=None) 
else: 
    print("Empty") 

,這是錯誤:

/Users/lixuefei/anaconda/bin/python3.5/Users/lixuefei/PycharmProjects/project/project.11.09.py 
    Traceback (most recent call last): 
    File "/Users/lixuefei/PycharmProjects/project/project.11.09.py", line 25, in <module> 
     df.to_csv(os.path.join(path,filename+filetype),header=None) 
    TypeError: Can't convert 'module' object to str implicitly 
+1

您將模塊分配給變量'filetype = csv' - 您忘記了''''''filetype =「csv」'或者事件'filetype =「。csv」' – furas

回答

1

一)空白是你的朋友。

b)filename是一個字符串,filetype是csv模塊。

我想你的意思說的是:

filetype = "csv" 

隨後的下面一個:

os.path.join(path, filename + "." + filetype) 

或更好:

os.path.join(path, "%s.%s" % (filename, filetype)) 

或 「正確」 的Python 3路:

os.path.join(path, "{}.{}".format(filename, filetype)) 
相關問題