Pandas:数据文件中没有列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38228098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: No column names in data file
提问by chintan s
I am trying to process a dataset to play with for DataScience but it does not have column names. The output of df.head()
as shown below:
我正在尝试处理要用于 DataScience 的数据集,但它没有列名。输出df.head()
如下图:
1 73 Not in universe 0 0.1 0.2 Not in universe.1
0 2 58 Self-employed-not incorporated 4 34 0 Not in universe
1 3 18 Not in universe 0 0 0 High school
2 4 9 Not in universe 0 0 0 Not in universe
3 5 10 Not in universe 0 0 0 Not in universe
4 6 48 Private 40 10 1200 Not in universe
What I would like to see is
我想看到的是
0 1 73 Not in universe 0 0.1 0.2 Not in universe.1
1 2 58 Self-employed-not incorporated 4 34 0 Not in universe
2 3 18 Not in universe 0 0 0 High school
3 4 9 Not in universe 0 0 0 Not in universe
4 5 10 Not in universe 0 0 0 Not in universe
5 6 48 Private 40 10 1200 Not in universe
I could assign random column names but is there a nicer way?
我可以分配随机列名,但有更好的方法吗?
回答by EdChum
You loaded the file without specifying whether it had a header row or not, by default it infers it from the first row, if it's missing then pass header=None
:
您加载文件时没有指定它是否有标题行,默认情况下它从第一行推断它,如果它丢失,则传递header=None
:
df = pd.read_csv(file_path, header=None)
回答by shinchaan
I would like you to go through this link. Default value for header is 'infer' which means it will automatically set the integer values for the data if not specify.
我希望你通过这个链接。header 的默认值为“infer”,这意味着如果未指定,它将自动设置数据的整数值。
Also you can set the different column names by setting names parameter which takes an array, list of column names.
您还可以通过设置采用数组、列名列表的 names 参数来设置不同的列名。