pandas 里面的含义

import pandas as pd
word = pd.read_table('test.txt', encoding = 'utf-8', names = ['query'])

这里的names 里的 ‘query’是什么意思??

header : int, list of ints, default ‘infer’

Row number(s) to use as the column names, and the start of the data. Defaults to 0 if no names passed, otherwise 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 are 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.

另外我在官网看到这里的headers 有点迷惑 ,“Row number(s) to use as the column names ” 行的数字当成列的名字 ,这怎么理解呢?

阅读 2.6k
1 个回答

最好的办法是来个小例子试一下,

假设你有一个data.cvs的逗号分隔的数据文件,内容如下

0     index,name,comment,,,,
1    1,name_01,coment_01,,,,
2    2,name_02,coment_02,,,,
3    3,name_03,coment_03,,,,
4    4,name_04,coment_04,,,,
5    5,name_05,coment_05,,,,

用下面的代码来读

import pandas as pd
word = pd.read_table('data.csv', delimiter=',',encoding = 'utf-8', names = ['index','name','comment','foo','bar','baz'], header=0)

print(word)

你将看到如下的结果:

      index       name  comment  foo  bar  baz
1   name_01  coment_01      NaN  NaN  NaN  NaN
2   name_02  coment_02      NaN  NaN  NaN  NaN
3   name_03  coment_03      NaN  NaN  NaN  NaN
4   name_04  coment_04      NaN  NaN  NaN  NaN
5   name_05  coment_05      NaN  NaN  NaN  NaN
......

回答你的问题:names是指读到内存后的数据的列名,heads是指数据表头行号,真正的数据是这一行之后开始。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题