Joel G Mathew
Joel G Mathew

Reputation: 8081

Inserting a header row for pandas dataframe

I have just started python and am trying to rewrite one of my perl scripts in python. Essentially, I had a long script to convert a csv to json.

I've tried to import my csv into a pandas dataframe, and wanted to insert a header row at the top, since my csv lacks that. Code:

import pandas
db=pandas.read_csv("netmedsdb.csv",header=None)
db

Output:

0   1   2   3
0   3M CAVILON NO STING BARRIER FILM SPRAY 28ML OTC 0   Rs.880.00   3M INDIA LTD
1   BACTI BAR SOAP 75GM OTC Rs.98.00    6TH SKIN PHARMACEUTICALS PVT LTD
2   KWIKNIC MINT FLAVOUR 4MG CHEW GUM TABLET 30'S   NICOTINE    Rs.180.00   A S V LABORATORIES INDIA PVT LTD
3   RIFAGO 550MG TABLET 10'S    RIFAXIMIN 550MG Rs.298.00   AAREEN HEALTHCARE
4   999 OIL 60ML    AYURVEDIC MEDICINE  Rs.120.00   AAKASH PHARMACEUTICALS
5   AKASH SOAP 75GM AYURVEDIC PRODUCT   Rs.80.00    AAKASH PHARMACEUTICALS
6   GROW CARE OIL 100ML AYURVEDIC MEDICINE  Rs.190.00   AAKASH PHARMACEUTICALS
7   GROW CARE OIL 100ML AYURVEDIC MEDICINE  Rs.190.00   AAKASH PHARMACEUTICALS
8   RHUNS OIL 30ML  AYURVEDIC   Rs.50.00    AAKASH PHARMACEUTICALS
9   VILLO CAPSULE 10'S  AYURVEDIC MEDICINE  Rs.70.00    AAKASH PHARMACEUTICALS
10  VITAWIN FORTE CAPSULE 10'S  AYURVEDIC MEDICINE  Rs.150.00   AAKASH PHARMACEUTICALS

I wrote the following code to insert the first element at row 0,coloumn 0:

db.insert(loc=0,column='0',value='Brand')
db

Output:

0   0   1   2   3
0   Brand   3M CAVILON NO STING BARRIER FILM SPRAY 28ML OTC 0   Rs.880.00   3M INDIA LTD
1   Brand   BACTI BAR SOAP 75GM OTC Rs.98.00    6TH SKIN PHARMACEUTICALS PVT LTD
2   Brand   KWIKNIC MINT FLAVOUR 4MG CHEW GUM TABLET 30'S   NICOTINE    Rs.180.00   A S V LABORATORIES INDIA PVT LTD
3   Brand   RIFAGO 550MG TABLET 10'S    RIFAXIMIN 550MG Rs.298.00   AAREEN HEALTHCARE
4   Brand   999 OIL 60ML    AYURVEDIC MEDICINE  Rs.120.00   AAKASH PHARMACEUTICALS
5   Brand   AKASH SOAP 75GM AYURVEDIC PRODUCT   Rs.80.00    AAKASH PHARMACEUTICALS
6   Brand   GROW CARE OIL 100ML AYURVEDIC MEDICINE  Rs.190.00   AAKASH PHARMACEUTICALS
7   Brand   GROW CARE OIL 100ML AYURVEDIC MEDICINE  Rs.190.00   AAKASH PHARMACEUTICALS
8   Brand   RHUNS OIL 30ML  AYURVEDIC   Rs.50.00    AAKASH PHARMACEUTICALS
9   Brand   VILLO CAPSULE 10'S  AYURVEDIC MEDICINE  Rs.70.00    AAKASH PHARMACEUTICALS
10  Brand   VITAWIN FORTE CAPSULE 10'S  AYURVEDIC MEDICINE  Rs.150.00   AAKASH PHARMACEUTICALS

But unfortunately I got the word "Brand" inserted at coloumn 0 in all rows.

I'm trying to add the header coloumns "Brand", "Generic", "Price", "Company"

Upvotes: 3

Views: 1636

Answers (1)

jezrael
jezrael

Reputation: 863741

Need parameter names in read_csv only:

import pandas as pd

temp=u"""a,b,10,d
e,f,45,r
"""
#after testing replace 'pd.compat.StringIO(temp)' to 'netmedsdb.csv'
df = pd.read_csv(pd.compat.StringIO(temp), names=["Brand", "Generic", "Price", "Company"])
print (df)
  Brand Generic  Price Company
0     a       b     10       d
1     e       f     45       r

Upvotes: 2

Related Questions