0

it's making extra columns for each time new data is saved in this sheet How can I stop making extra columns?

enter image description here

Data = [[Category,Headlines,Author,Source,Published_Date,Feature_Image,Content,url]]

cols = ['Category','Headlines','Author','Source','Published_Date','Feature_Image','Content','URL']
try:
    opened_df = pd.read_csv('C:/Users/Public/pagedata.csv')
    opened_df = pd.concat([opened_df,pd.DataFrame(Data, columns = cols)])
except:
    opened_df = pd.DataFrame(Data, columns = cols)

opened_df.to_csv('C:/Users/Public/pagedata.csv')
BigBen
  • 46,229
  • 7
  • 24
  • 40
Info Rewind
  • 145
  • 7
  • https://stackoverflow.com/questions/20845213/how-to-avoid-pandas-creating-an-index-in-a-saved-csv, I guess. – BigBen Oct 05 '22 at 18:12

1 Answers1

1

Try this in your last line of your code:

opened_df.to_csv('C:/Users/Public/pagedata.csv', index = False)

in case the "Unnamed: 0" gets created everytime and is an unwanted column in your csv try this, before the last line in your code:

opened_df.drop('Unnamed: 0', axis = 1, inplace = True)
opened_df.to_csv('C:/Users/Public/pagedata.csv', index = False)

i hope this solves your issue.

Echo
  • 293
  • 2
  • 10