Search code examples
pythonpandasdataframeexport-to-csv

I want to save my scrape record in csv, this Python Code working fine for me but it's making extra columns each time new data is saved


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

Solution

  • 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.