user105939
user105939

Reputation: 219

Failing to index my column names

this is my csv file: https://drive.google.com/file/d/1WLjgGvp8kn072n7IlxVZdk4vIO54LxQz/view?usp=sharing

Using python3 I do:

import pandas as pd
df=pd.read_csv('Battery.csv', sep=',',header=1)
df.values
df.head()

I would expect the headers to be listed using df.head() , but something is very wrong, and all are listed as 40 unnamed columns:

>>> df.head()

    13  380.38  18.4  6.99899199999999  Unnamed: 4  Unnamed: 5  Unnamed: 6  \
0  123     NaN   NaN               NaN         NaN         NaN         NaN   
1  218     NaN   NaN               NaN         NaN         NaN         NaN   
2  319  379.89  26.5         10.067085         NaN         NaN         NaN   
3  739  380.01  21.7          8.246217         NaN         NaN         NaN   
4  863  380.40  22.3          8.482920         NaN         NaN         NaN   

   Unnamed: 7  Unnamed: 8  Unnamed: 9     ...       Unnamed: 30  Unnamed: 31  \
0         NaN         NaN         NaN     ...               NaN       0.3538   
1         NaN         NaN         NaN     ...               NaN       0.3538   
2         NaN         NaN         NaN     ...               NaN          NaN   
3         NaN         NaN         NaN     ...               NaN          NaN   
4         NaN         NaN         NaN     ...               NaN          NaN   

Where/how did I go wrong ?

Upvotes: 0

Views: 35

Answers (1)

jezrael
jezrael

Reputation: 863166

Use only because sep=',' and header=0 are default parameters in read_csv:

sep : str, default ','

Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\r\t'

header : int or list of ints, default 'infer'

Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of integers that specify row locations for a multi-index on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file.


df = pd.read_csv('Battery.csv')

print (df.head())

   Time  Battery voltage  Battery current  Battery power  DC-DC current  \
0    13           380.38             18.4       6.998992            NaN   
1   123              NaN              NaN            NaN            NaN   
2   218              NaN              NaN            NaN            NaN   
3   319           379.89             26.5      10.067085            NaN   
4   739           380.01             21.7       8.246217            NaN   

   DC-DC voltage  DC-DC input power  DC-DC output power  Fr drive power max  \
0            NaN                NaN                 NaN                 NaN   
1            NaN                NaN                 NaN                 NaN   
2            NaN                NaN                 NaN                 NaN   
3            NaN                NaN                 NaN                 NaN   
4            NaN                NaN                 NaN                 NaN   

   Rr regen power max     ...       Full typical range  Cell temp min  \
0                 NaN     ...                      NaN            NaN   
1                 NaN     ...                      NaN         0.3538   
2                 NaN     ...                      NaN         0.3538   
3                 NaN     ...                      NaN            NaN   
4                 NaN     ...                      NaN            NaN   

   Cell temp avg  Cell temp max  Cell temp diff  Cell min  Cell avg  Cell max  \
0            NaN            NaN             NaN       NaN       NaN       NaN   
1          1.342         3.5868           3.233   3.84056  3.895028   3.96683   
2          1.342         3.5868           3.233   3.84056  3.898056   3.96683   
3            NaN            NaN             NaN       NaN       NaN       NaN   
4            NaN            NaN             NaN       NaN       NaN       NaN   

   Cell diff  Unnamed: 39  
0        NaN          NaN  
1    0.12627          NaN  
2    0.12627          NaN  
3        NaN          NaN  
4        NaN          NaN  

[5 rows x 40 columns]

Upvotes: 1

Related Questions