2016-06-22 2 views
1

私は同時に持っているすべてのファイルに書き込もうとします。 は、私はいくつかのファイル同時に別のものに書き込む方法Pythonを使用する

[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected] 
[email protected] 

を持っていると私は、これにデータを書きたいです。しかし、どのように私は機能に送信(と私はGROUPBYと値をファイルに書き込む必要がある)ことができます

df = pd.read_excel('group.xlsx') 
def add_xlsx_sheet(df, sheet_name=u'Смартфоны полно', index=True, digits=1, path='[email protected]'): 
    book = load_workbook(path) 
    writer = ExcelWriter('[email protected]', engine='openpyxl') 
    writer.book = book 
    writer.sheets = dict((ws.title, ws) for ws in book.worksheets) 
    if sheet_name in list(writer.sheets.keys()): 
     sh = book.get_sheet_by_name(sheet_name) 
     book.remove_sheet(sh) 
    df.to_excel(writer, sheet_name=u'Смартфоны полно', startrow=0, startcol=0, 
      float_format='%.{}f'.format(digits), index=index) 
    writer.save() 

これは、1つのファイルに動作しますが、それは、このファイルにすべてのデータを書き込みます。しかし、私はメールでidはどのように私は、関数内のすべてのファイルを指定することができ、ファイル の名前を遵守し、次の

df.groupby('member_id').apply(lambda g: g.to_excel(str(g.name) + '.xlsx', 'sheet2')) 

答えて

0

問題がdf.groupby('col_name').apply(lambda x: add_xlsx_sheet(x, x.name, path='{}.xlsx'.format(x.name)))

で解決されたグループを、記述する必要があります
関連する問題