0

I have a txt file which does not have column names. When I try importing it using pandas.read_csv, the first row becomes the column names.

the txt file is such as:

0 9 8 2 1

1 2 3 2 2

2 3 4 5 3

How can I import this txt file successfully without the first row (0 9 8 2 1) being column names for the data frame?

And how can I insert the column names in the data frame?

cs95
  • 379,657
  • 97
  • 704
  • 746
Seongin Na
  • 13
  • 1
  • 2

1 Answers1

1
  1. If no header: Load csv with no header using pandas read_csv If your csv file does not have header, then you need to set header = None while reading it .

  2. To pass columns: pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']] for columns in ['foo', 'bar'] order or pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']] for ['bar', 'foo'] order.

pd.read_csv('data.csv', header=None, usecols=(COL-ARRAY-IN-ORDER')

mamtach
  • 88
  • 3
  • 13