鍍金池/ 問答/數(shù)據(jù)分析&挖掘  Python/ pandas 里面的含義

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.

另外我在官網(wǎng)看到這里的headers 有點迷惑 ,“Row number(s) to use as the column names ” 行的數(shù)字當成列的名字 ,這怎么理解呢?

回答
編輯回答
念初

最好的辦法是來個小例子試一下,

假設你有一個data.cvs的逗號分隔的數(shù)據(jù)文件,內(nèi)容如下

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)

你將看到如下的結(jié)果:

      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是指讀到內(nèi)存后的數(shù)據(jù)的列名,heads是指數(shù)據(jù)表頭行號,真正的數(shù)據(jù)是這一行之后開始。

2017年9月15日 02:50