Attempting to add a column heading to the newly created csv file

Question:

I’m trying to add the add the header to my csv file that I created in the code given below:
This is the code im using

There’s only 1 column in the csv file that I’m trying to create,
the data frame consists of an array, the array is
[0.6999346, 0.6599296, 0.69770324, 0.71822715, 0.68585426, 0.6738229, 0.70231324, 0.693281, 0.7101939, 0.69629824]
i just want to create a csv file with header like this

Desired csv File , I want my csv file in this format

enter image description here

Please help me with detailed code, I’m new to coding.

I tried this

df = pd.DataFrame(c)
df.columns = ['Confidence values']
pd.DataFrame(c).to_csv('/Users/sunny/Desktop/objectdet/final.csv',header= True , index= True)

Im getting this

But i’m getting this csv file

Asked By: Sarathchandra M

||

Answers:

Try this

import pandas as pd
array = [0.6999346, 0.6599296, 0.69770324, 0.71822715, 0.68585426, 0.6738229, 0.70231324, 0.693281, 0.7101939, 0.69629824]
df = pd.DataFrame(array)
df.columns = ['Confidence values']
df.to_csv('final.csv', index=True, header=True)

Your action pd.DataFrame(c) is creating a new dataframe with no header, while your df is a dataframe with header.

You are writing the dataframe with no header to a csv, that’s why you dont get your header in your csv. All you need to do is replace pd.DataFrame(c) with df

Answered By: Bao Le