熊猫read_html如何删除索引?

时间:2019-01-20 03:59:41

标签: pandas dataframe

我正在努力从数据框中删除索引列。

通常,当我读取一个csv文件时,可以将index设置为False或index_col = 0,然后删除index列。但是由于某种原因,我无法阅读html。有任何想法吗?我也尝试过reset_index(drop=True)。我不想将任何列设置为索引。

path = 'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M' 

canada = pd.read_html(path)    

cn_table=canada[0]

1 个答案:

答案 0 :(得分:0)

IIUC,您希望第一行作为标题,请使用header=0

canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0, flavor = 'bs4')

或者:

canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0)

cn_table=canada[0]
>>cn_table

    Postcode    Borough          Neighbourhood
0   M1A         Not assigned     Not assigned
1   M2A         Not assigned     Not assigned
2   M3A         North York       Parkwoods
3   M4A         North York       Victoria Village
4   M5A         Downtown Toronto Harbourfront
5   M5A         Downtown Toronto Regent Park

... ... ... ...
288 M9Z         Not assigned     Not assigned

要使用索引将数据帧保存到csv中,请使用

cn_table.to_csv('path+filename.csv',index=False)