读取CSV文件时将数据框导入列的第一个值作为列名

时间:2019-10-09 13:22:41

标签: python pandas dataframe

每当我使用org-table-convert-region is an interactive Lisp closure in ‘../org-mode/lisp/org-table.el’. (org-table-convert-region BEG0 END0 &optional SEPARATOR) Convert region to a table. The region goes from BEG0 to END0, but these borders will be moved slightly, to make sure a beginning of line in the first line is included. SEPARATOR specifies the field separator in the lines. It can have the following values: (4) Use the comma as a field separator (16) Use a TAB as field separator (64) Prompt for a regular expression as field separator integer When a number, use that many spaces, or a TAB, as field separator regexp When a regular expression, use it to match the separator nil When nil, the command tries to be smart and figure out the separator in the following way: - when each line contains a TAB, assume TAB-separated material - when each line contains a comma, assume CSV material - else, assume one or more SPACE characters as separator. 读取没有列名的csv文件时,第一个值都会转换为列名。仅使用Python可以采取哪些措施(如果可以完成)以纠正这种情况。请不要建议手动打开文件,然后在其中放置列名。

3 个答案:

答案 0 :(得分:1)

您可以使用关键字arg标头。

pd.read_csv('file_name.csv', header=None)

通过这种方式,列名将是从0开始的数字。您还可以通过例如传递自己的列名。

pd.read_csv('file_name.csv', header=None, names=['col1','col2'])

答案 1 :(得分:1)

您需要添加参数header并传递None

df = pd.read_csv('PATH',header=None)

从熊猫read_csv

  

header:整数,整数列表,默认为“推断”   行号(用作列名)以及数据的开头。默认行为是推断列名:如果未传递名称,则行为与header = 0相同,并且从文件的第一行推断出列名;如果显式传递列名,则该行为与header = None相同。 。显式传递header = 0以便能够替换现有名称。标头可以是整数列表,这些整数指定列上的多索引的行位置,例如[0,1,3]。未指定的中间行将被跳过(例如,在本示例中为2)。请注意,如果skip_blank_lines = True,则此参数将忽略注释行和空行,因此header = 0表示数据的第一行,而不是文件的第一行。

答案 2 :(得分:1)

您尝试过

pd.read_csv(file_name, header=None)

pd.read_csv(file_name, names=['col1', 'col2'])

相关问题