Python Forum
How to store columns of a .csv in variables (when the .csv has no headers) ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to store columns of a .csv in variables (when the .csv has no headers) ?
#4
That depends on what you want. You can read a CSV file without a header, but your column index will be numbers, 0, 1, 2... just as you get for the row index. To read a CSV file that has no header, again we return to the excellent Pandas documentation.

https://pandas.pydata.org/docs/reference...d_csv.html

This time it is not quite as excellent as before.

Quote:header: int, list of int, None, 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.

Our choices are:
int : Use this line in the file as the column header.
list of int: Use these lines in the file as the column header (for multi-index headers).
None: Not described very well what happens, but sounds like it will use "names" provided. What does it do if we don't provide names?
default: Infer column header names from the file.

So if you have a CSV file with no header, and you want Pandas to generate column numbers instead of inferring names from the file, set "header = None".
df = pd.read_csv(filename, header=None)
hobbyist likes this post
Reply


Messages In This Thread
RE: How to store columns of a .csv in variables (when the .csv has no headers) ? - by deanhystad - Aug-18-2023, 08:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TreeView column headers TWB 2 8,488 Jan-29-2023, 02:13 PM
Last Post: TWB
  export sql table to csv using BCP with headers mg24 0 1,244 Jan-19-2023, 05:36 AM
Last Post: mg24
  Code changing rder of headers Led_Zeppelin 0 1,492 Jul-13-2022, 05:38 PM
Last Post: Led_Zeppelin
  making variables in my columns and rows in python kronhamilton 2 2,461 Oct-31-2021, 10:38 AM
Last Post: snippsat
  Request Headers (scheme) JohnnyCoffee 0 2,447 Mar-31-2021, 09:17 PM
Last Post: JohnnyCoffee
  Reading csv with multiple "headers" Clives 3 3,670 Dec-31-2020, 09:25 AM
Last Post: Ana_junior
  module to store functions/variables and how to call them? mstichler 3 3,507 Jun-03-2020, 06:49 PM
Last Post: mstichler
  I can't use file __init__ to store shared variables and classes in the package AlekseyPython 2 4,209 Feb-04-2019, 06:26 AM
Last Post: AlekseyPython
  Receive Serial Data and store in different Variables in Python jenkins43 5 7,429 Dec-28-2018, 01:33 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020