6

I want to read from a CSV file using pandas read_csv. The CSV file doesn't have column names. When I use pandas to read the CSV file, the first row is set as columns by default. But when I use df.columns = ['ID', 'CODE'], the first row is gone. I want to add, not replace.

df = pd.read_csv(CSV)
df

    a   55000G707270
0   b   5l0000D35270
1   c   5l0000D63630
2   d   5l0000G45630
3   e   5l000G191200
4   f   55000G703240


df.columns=['ID','CODE']
df

    ID          CODE
0   b   5l0000D35270
1   c   5l0000D63630
2   d   5l0000G45630
3   e   5l000G191200
4   f   55000G703240
1

5 Answers 5

13

I think you need parameter names in read_csv:

df = pd.read_csv(CSV, names=['ID','CODE'])

names : array-like, default None

List of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list are not allowed unless mangle_dupe_cols=True, which is the default.

Sign up to request clarification or add additional context in comments.

Comments

2

The reason there are extra index columns add is because to_csv() writes an index per default, so you can either disable index when saving your CSV:

df.to_csv('file.csv', index=False)

or you can specify an index column when reading:

df = pd.read_csv('file.csv', index_col=0)

Comments

1

You may pass the column names at the time of reading the csv file itself as :

df = pd.read_csv(csv_path, names = ["ID", "CODE"])

Comments

1

Use names argument in function call to add the columns yourself:

df = pd.read_csv(CSV, names=['ID','CODE'])

Comments

1

you need both: header=None and names=['ID','CODE'], because there are no column names/labels/headers in your CSV file:

df = pd.read_csv(CSV, header=None, names=['ID','CODE'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.