Evgeny Romanenkov
Evgeny Romanenkov

Reputation: 11

Can't drop pandas column

I have a pandas dataframe that I need to slice into several tables based on values and save into several .csv files. This method seems to work, however it creates a column (first one) that I cannot drop (instead drops 2nd column). Could someone tell me why it is there and how can I get rid of it? Thank you. Here is the code:

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')

Upvotes: 0

Views: 411

Answers (1)

jezrael
jezrael

Reputation: 863531

First column in DataFrame is called index.

You need parameter index=False in to_csv for omit it:

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

Or better:

Instead:

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')

use groupby for looping:

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

Upvotes: 1

Related Questions