Give column name when read csv file pandas

PythonPandas

Python Problem Overview


This is the example of my dataset.

>>> user1 = pd.read_csv('dataset/1.csv')
>>> print(user1)
          0  0.69464   3.1735   7.5048
0  0.030639  0.14982  3.48680   9.2755
1  0.069763 -0.29965  1.94770   9.1120
2  0.099823 -1.68890  1.41650  10.1200
3  0.129820 -2.17930  0.95342  10.9240
4  0.159790 -2.30180  0.23155  10.6510
5  0.189820 -1.41650  1.18500  11.0730

How to push down the first column and add the names column [TIME, X, Y, and Z] on the first column.

The desired output is like this:

       TIME        X        Y        Z
0         0  0.69464   3.1735   7.5048
1  0.030639  0.14982  3.48680   9.2755
2  0.069763 -0.29965  1.94770   9.1120
3  0.099823 -1.68890  1.41650  10.1200
4  0.129820 -2.17930  0.95342  10.9240
5  0.159790 -2.30180  0.23155  10.6510
6  0.189820 -1.41650  1.18500  11.0730

Python Solutions


Solution 1 - Python

I'd do it like this:

colnames=['TIME', 'X', 'Y', 'Z'] 
user1 = pd.read_csv('dataset/1.csv', names=colnames, header=None)

Solution 2 - Python

If we are directly use data from csv it will give combine data based on comma separation value as it is .csv file.

user1 = pd.read_csv('dataset/1.csv')

If you want to add column names using pandas, you have to do something like this. But below code will not show separate header for your columns.

col_names=['TIME', 'X', 'Y', 'Z'] 
user1 = pd.read_csv('dataset/1.csv', names=col_names)

To solve above problem we have to add extra filled which is supported by pandas, It is header=None

user1 = pd.read_csv('dataset/1.csv', names=col_names, header=None)

Solution 3 - Python

we can do it with a single line of code.

 user1 = pd.read_csv('dataset/1.csv', names=['TIME', 'X', 'Y', 'Z'], header=None)

Solution 4 - Python

In addition to above answers if your dataset already has column names and you want to replace it with your custom names then set header=0 instead of header=None

Solution 5 - Python

user1  = pd.read_csv('dataset/1.csv',  names=['Time',  'X',  'Y',  'Z']) 

names parameter in read_csv function is used to define column names. If you pass extra name in this list, it will add another new column with that name with NaN values.

header=None is used to trim column names is already exists in CSV file.

Solution 6 - Python

Use nrows=1 to avoid consuming loading time if the dataset is too large.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmarkov zainView Question on Stackoverflow
Solution 1 - Pythonwmoco_6725View Answer on Stackoverflow
Solution 2 - PythonJitesh MohiteView Answer on Stackoverflow
Solution 3 - PythonHariharan ARView Answer on Stackoverflow
Solution 4 - PythonMohit BurkuleView Answer on Stackoverflow
Solution 5 - PythonMahendraView Answer on Stackoverflow
Solution 6 - PythonJiaxiangView Answer on Stackoverflow