1

I csv have a file with data lines, but it misses the label line. This label line is a string, coma separated, ready to be appended on top.

import pandas as pd
data = pd.read_csv("C:\\Users\\Desktop\\Trades-List.txt", sep=',')
labelLine = "label1,label2,label3"

How to add the 'labelLine' on top of data and make sure it is properly considered as the label line of the file?

Robert Brax
  • 6,508
  • 12
  • 40
  • 69
  • Does this answer your question? [Pandas read in table without headers](https://stackoverflow.com/questions/29287224/pandas-read-in-table-without-headers) – Alex Mar 14 '23 at 11:41

2 Answers2

1

Parameter sep=',' is default, so should be removed, add names with splitted values for columns names:

labelLine = "label1,label2,label3"
data = pd.read_csv("C:\\Users\\Desktop\\Trades-List.txt", names=labelLine.split(','))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Use the names parameter of read_csv after splitting to list with str.split:

import pandas as pd

labelLine = "label1,label2,label3"

data = pd.read_csv("C:\\Users\\Desktop\\Trades-List.txt", sep=',',
                   names=labelLine.split(','))

mozway
  • 194,879
  • 13
  • 39
  • 75