2017-05-03 83 views
0

我有一個熊貓數據框,我需要根據數值切分成幾個表格並保存到多個.csv文件中。這個方法似乎可行,但是它創建了一個我不能放下的列(第一個)(而不是第二列)。有人能告訴我爲什麼它在那裏,我該如何擺脫它?謝謝。下面是代碼:不能丟棄熊貓列

new_d['Supplier'] = new_d.apply(lambda row: determine_supplier(row), axis = 1) 
new_d.sort_values(by = ['Supplier'], inplace = True) 
new_d.set_index(keys = ['Supplier'], drop = False, inplace = True) 
suppliers = new_d['Supplier'].unique().tolist() 
for supplier in suppliers: 
    po = new_d.loc[new_d.Supplier == supplier] #the problem is here? 
    po = po.drop(po.columns[[0]], axis = 1) # can't drop 
    po.to_csv(path_or_buf = r'PO\\' + supplier + '_PO.csv') 

回答

1

DataFrame一列稱爲index

您需要to_csv參數index=False的忽略它:

po.to_csv(path_or_buf = r'PO\\'+ supplier+'_PO.csv',index=False) 

或者更好:

相反:

for supplier in suppliers: 
    po = new_d.loc[new_d.Supplier == supplier] #the problem is here? 
    po = po.drop(po.columns[[0]], axis = 1) # can't drop 
    po.to_csv(path_or_buf = r'PO\\' + supplier + '_PO.csv') 

使用groupbylooping

for supplier, po in new_d.groupby('Supplier'): 
    po.to_csv(r'PO\\'+ supplier +'_PO.csv',index=False)