3
我正在重新索引多個文件夾中的文件。一個文件最初看起來是這樣的:將列設置爲索引時Dtype發生更改
Combined Percent
0101 50
0102 25
0104 25
然後我用這個代碼創建一個新的指數,這是我的所有文件的索引的文件夾中的工會:
import pandas as pd
from glob import glob
folders=(r'C:\pathway_to_folders')
for folder in os.listdir(folders):
path=os.path.join(folders,folder)
filenames=glob(os.path.join(path+'/*.csv'))
def rfile(fn):
return pd.read_csv(fn, dtype='str', index_col=0)
dfs = [rfile(fn) for fn in filenames]
idx = dfs[0].index
for i in range(1, len(dfs)):
idx = idx.union(dfs[i].index)
print idx
當我設置作爲索引列列Combined
,dfs
現在看起來是這樣的:
Combined Percent
101 50
102 25
104 25
有沒有一種方法,以保持格式化爲指數一樣的原始列,或MANIP ulate我的代碼不必設置索引可能?
它應該只是工作,'pd.read_csv(fn,dtype = {'Combined':str},index_col = 0)'工作嗎? – EdChum
它沒有。可能是因爲數據幀正在變成這個行的列表:'dfs = [rfile(fn)for fn in filenames]''? –
如果你沒有設置index_col,dtype是否保存? 'pd.read_csv(fn,dtype = str)'? – EdChum