0

when i am inserting data first time in csv file it is good but on second time it again inserts column name

import pandas as pd




name = input("Enter student name")

print("")
print("enter marks info below")
print("")
eng= input("enter English mark : ")
maths= input("enter Maths mark : ")
physics= input("enter Physics mark : ")
chemistry= input("enter Chemistry mark : ")
ip= input("enter Informatic Practices mark : ")

dict = {
        "name":{name:name},
        "english":{name:eng},
        "maths":{name:maths},
        "physics":{name:physics},
        "chemistry":{name:chemistry},
        "ip":{name:ip}
    }
df= pd.DataFrame(dict)


df.to_csv("hello.csv", sep="|",index=False,na_rep="null",mode='a')
print("hello.csv")
read = pd.read_csv("hello.csv", sep='|')

print(read)

data in csv file :

name|english|maths|physics|chemistry|ip
dddd|dd|e3|3|3|3
name|english|maths|physics|chemistry|ip 
ddddddd|e33|33|3||3
name|english|maths|physics|chemistry|ip
dddddd|33|333||3|

please help in solving how to fix so that column not get added multiple time

David Specht
  • 7,784
  • 1
  • 22
  • 30

2 Answers2

0

you can read the csv file everytime before your run this script.

import pandas as pd
import os

df = pd.DataFrame() if not os.path.exists("hello.csv") else pd.read_csv("hello.csv", sep='|')

name = input("Enter student name")
print("")
print("enter marks info below")
print("")
eng = input("enter English mark : ")
maths = input("enter Maths mark : ")
physics = input("enter Physics mark : ")
chemistry = input("enter Chemistry mark : ")
ip = input("enter Informatic Practices mark : ")

dict = {
    "name": {name: name},
    "english": {name: eng},
    "maths": {name: maths},
    "physics": {name: physics},
    "chemistry": {name: chemistry},
    "ip": {name: ip}
}
df = df.append(pd.DataFrame(dict))

df.to_csv("hello.csv", sep="|", index=False, na_rep="null", mode='w')
print("hello.csv")
read = pd.read_csv("hello.csv", sep='|')

print(read)

and you can also use this code below to export df without columns, but may still have to check file existence or columns sequence first.

df.to_csv('filename.csv', header=False, sep='|', mode='a')

Eason
  • 1
  • 1
0

The output file is being opened in append mode, and to_csv() will include the header row by default. You could simply turn off the header:

df.to_csv("hello.csv", header=False, sep="|", index=False, na_rep="null", mode='a')

which will work, but your CSV file will not have a header row. If you require the header then you could check whether the output CSV file already exists and disable headers if it does.

One way is that suggested in the answer by @Eason, however, that might not be practical for large files due to the extra loading time (possibly this is the reason why you are using append mode?)

The following disables the CSV headers if the file already exists and is not empty.

from pathlib import Path

header = True
p = Path('hello.csv')
if p.exists() and p.stat().st_size:
    header = False

df.to_csv(p, header=header, sep="|", index=False, na_rep="null", mode='a')
mhawke
  • 84,695
  • 9
  • 117
  • 138