0

I try to import the file data.csv into a pd.DataFrame where the column names are placed in a commented line. Using data = pd.read_csv ('data.csv', sep = ',', skiprows = 1, thousands = None, decimal = '.') doesn't remove the comment sign (//) from the header. When I say comment = '//', I don't get the column names, which can vary from file to file.

How do I remove the comment sign from the first column name?

My data.csv:

//general comment, shouldn't pe parsed
//<d_d>,<a_d>,<a_a>
7199.71,173.965,7279.85
5671.23,1576.78,5272.2
6905.44,-1008.56,6864.76
7701.41,-90.0966,8611.31
7253.81,-377.103,2863.48
4745.01,1951.36,4020.75

The resulting pd.DataFrame:

      //<d_d>      <a_d>     <a_a>
0     7199.71   173.9650   7279.85
1     5671.23  1576.7800   5272.20
2     6905.44 -1008.5600   6864.76
3     7701.41   -90.0966   8611.31
4     7253.81  -377.1030   2863.48
5     4745.01  1951.3600   4020.75
Community
  • 1
  • 1
Nepumuk
  • 243
  • 1
  • 3
  • 13
  • 1
    skiprows=1 if it's always the first row. if it's not then you're out of luck. – cs95 Feb 15 '20 at 15:50
  • Does this answer your question? [Renaming columns in pandas](https://stackoverflow.com/questions/11346283/renaming-columns-in-pandas) – AMC Feb 16 '20 at 04:56
  • @cs95 As far as I can see, this doesn't play a role (yet) for my specific task I'm facing But it's still a goot point you made. – Nepumuk Feb 16 '20 at 09:25

1 Answers1

-1

If we have a dataframe, data, that looks like this:

   //col1  col2
0       1     1
1       2     2
2       3     3
3       4     4

And then we do this:

data.columns = data.columns.str.replace('//','')

We will get this:

   col1  col2
0     1     1
1     2     2
2     3     3
3     4     4
Daniel
  • 3,228
  • 1
  • 7
  • 23
  • 1
    The problem lies before you actually get the column names, right now the comment is read in as the column names. So your solution would not work – Erfan Feb 15 '20 at 15:55
  • This doesn't explain how the data is read (to remove the comment line without the header) which is why I suspect somebody downvoted this – cs95 Feb 15 '20 at 15:55