Give column name when read csv file pandas

前端 未结 4 1243
你的背包
你的背包 2021-01-30 19:39

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.0306         


        
相关标签:
4条回答
  • 2021-01-30 20:09
    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.

    0 讨论(0)
  • 2021-01-30 20:21

    I'd do it like this:

    colnames=['TIME', 'X', 'Y', 'Z'] 
    user1 = pd.read_csv('dataset/1.csv', names=colnames, header=None)
    
    0 讨论(0)
  • 2021-01-30 20:30

    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)
    
    0 讨论(0)
  • 2021-01-30 20:32

    we can do it with a single line of code.

     user1 = pd.read_csv('dataset/1.csv', names=['TIME', 'X', 'Y', 'Z'], header=None)
    
    0 讨论(0)
提交回复
热议问题