2016-05-20 11 views
0

私のプログラムは、ディレクトリ内の特定の仕様を満たすファイルを取得し、ラベルを読み込み、指定されたものに列を絞り込み、ファイルを要約するcsvを作成します。 Axesのラベルを読み込み、ファイルの最初の行に印字し、その後の各行に問題の軸の手段を表示するようにします。Pandasに軸指定のエラーがありません

エラーが発生するのは、そのような軸がないということです。エラーを検出したときに、データフレームに列を追加すると、そのエラーが発生しているようです。私はちょうど既存のものを求めています。要求された、私のDEFSとして

file_list = [f for f in os.listdir(pathname) if f.lower().endswith('.xls') and not 'map' in f.lower() and not 'check' in f.lower()] 
temp_df = reduce_df(read_in(file_list[0])) 
labels = temp_df.columns 
print labels # I printed this to the screen for troubleshooting purposes 
with open(summary_file, 'wb') as outfile: 
    writer = csv.writer(outfile) 
    writer.writerow(labels) 

    for f in file_list: 
     temp_df = reduce_df(read_in(f)) 
     print temp_df.columns#ditto. this should match the previous one   
     new_row = [temp_df.mean(col) for col in temp_df.columns] 
     writer.writerow(new_row) 

-output-

Using Python parser to sniff delimiter 
Index([u'Product Mass Flow (kg/hr)', u'TC 03 (C)', u'MKS NO ppm (-)', u'MKS NO2 ppm (-)', u'MKS NH3 ppm (-)', u'MKS N2O ppm (-)', u'MKS H2O (%)', u'NOx Calc MKS (-)', u'MKS2 NO ppm (-)', u'MKS2 NO2 ppm (-)', u'MKS2 NH3 ppm (-)', u'MKS2 N2O ppm (-)', u'MKS2 H2O (%)', u'NOx Calc MKS2 (-)'], dtype='object') 
Using Python parser to sniff delimiter 
Index([u'Product Mass Flow (kg/hr)', u'TC 03 (C)', u'MKS NO ppm (-)', u'MKS NO2 ppm (-)', u'MKS NH3 ppm (-)', u'MKS N2O ppm (-)', u'MKS H2O (%)', u'NOx Calc MKS (-)', u'MKS2 NO ppm (-)', u'MKS2 NO2 ppm (-)', u'MKS2 NH3 ppm (-)', u'MKS2 N2O ppm (-)', u'MKS2 H2O (%)', u'NOx Calc MKS2 (-)'], dtype='object') 
Traceback (most recent call last): 
    File "(program trace here)", line 50, in <module> 
    new_row = [temp_df.mean(col) for col in temp_df.columns] 
    File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 3490, in stat_func 
    skipna=skipna, numeric_only=numeric_only) 
    File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 3961, in _reduce 
    axis = self._get_axis_number(axis) 
    File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 295, in _get_axis_number 
    .format(axis, type(self))) 
ValueError: No axis named Product Mass Flow (kg/hr) for object type <class 'pandas.core.frame.DataFrame'> 

- データロガーは、紛らわしい名前を持っているか、単に必要はありませんその中に原料の束を持っています。私は私が集計するだけのものに削減しています:

def read_in(filename): 
    current_df = pd.read_csv(filename, sep = None, skiprows = range(11))  
    return current_df 

def reduce_df(temp_df): 
    columns = temp_df.columns 
    columns = [column for column in columns if 'mks' in column.lower() or 'mass flow' in column.lower() or 'tc 03' in column.lower()] 
    return temp_df[columns] 
+0

私が思うに、我々は'reduce_df'のソースが必要です。また、あなた自身のループをローリングするのではなく、 'df.to_csv'を使用しない理由があれば、どうでしょうか? –

+0

ああ、私はパンダにはまったく新しいものなので、以前はこの方法を使っていたので、まだその側面を探っていません。 – mauve

+0

それはおそらく行く方法です。 –

答えて

0

私は(私が説明するとto_csvになります)、それを考え出すことになったが、ここで現在働いているものです:

with open(summary_file, 'wb') as outfile: 
writer = csv.writer(outfile) 
writer.writerow(labels) 

for f in file_list: 
    temp_df = reduce_df(read_in(f)) 
    new_row = [value for value in temp_df.mean()] #if you don't do the value for value part, you end up with the column and value for each all in one cell 
    new_row.insert(0, f) #I added this to include the filename in the summary file for the row the stuff in that row applied to  
    writer.writerow(new_row) 
関連する問題