2017-11-26 18 views
0

私は列と行を削除し、列と行をリセットしてみます。しかし、3番目のファイルに、私はエラーを取得する:私のフォルダにIndexError:インデックス164は、サイズ164の軸1の範囲外です。

IndexError: index 164 is out of bounds for axis 1 with size 164

ファイルは167*164あると、落下した後、95*95になっています。私はあなたがあなたのソリューションを簡素化することができると信じて

path = r'/home/RESULTS' 
allFiles = sorted((glob.glob(path + "/*.csv"))) 
for file_ in allFiles: 
    data = pd.read_csv(file_, header=None) 
    data = data.astype(str) 
    data = data.apply(lambda x: x.str.replace(',', '.')) 
    data = data.astype(float) 
    data = data.fillna(value=1) 
    data.drop(data.columns[cols_to_drop],axis=1,inplace=True) 
    data.drop(data.index[rows_to_drop], inplace=True) 
    data = data.reset_index(drop=True) 
    data = data.T.reset_index(drop=True).T 
    print(file_) 

答えて

0

path =r'/home/RESULTS' 
allFiles = sorted((glob.glob(path + "/*.csv"))) 
for file_ in allFiles: 
    #decimal for convert float , to . 
    data = pd.read_csv(file_, header=None, decimal=',') 
    data = data.fillna(value=1) 
    #not necessary select columns and index, also ignore drop non exist values 
    data.drop(cols_to_drop,axis=1,inplace=True, errors='ignore') 
    data.drop(rows_to_drop, inplace=True, errors='ignore') 
    data=data.reset_index(drop=True) 
    #assign to columns range - reset columns names 
    data.columns = range(len(data.columns)) 
    print(data) 
+0

はあなたに感謝し、それでも同じエラー – Debra

+0

問題はデータです。 '3.'ファイルには' 164行しか含まれていないので、 '165'行は存在しません(最初の行は' 0'、 '165'行のインデックスは' 164'です) – jezrael

+0

別のエラー?これは、 'index'または' columns'に '164 165 166'の値がないことを意味します。しかし、解決策は存在しない値を無視するために、 'data.drop(in rows => true、errors = 'ignore')'と 'data.drop(cols_to_drop、axis = 1、inplace = True、errors = 'ignore')です。 。 – jezrael

関連する問題