2016-12-29 27 views
1

我想改变数据框对象。我想将第一行作为列索引。第一列作为行索引。pythonic方式使行作为列索引和列作为行索引

import pandas as pd 

wiki = "https://en.wikipedia.org/wiki/List_of_state_and_union_territory_capitals_in_India" 
df = pd.read_html(wiki)[1] 
df2 = df.copy() 
df2.head() 

enter image description here

目前我正在做这样的(我失去在这一行索引名):

df2.columns = df.iloc[0] 
df2.drop(0, inplace=True) 
df2.drop('No.', axis=1, inplace=True) 
df2.head() 

enter image description here

我该怎么办呢以更多的Pythonic方式保存行索引名称?

回答

2

您可以在read_html您的意愿直接指定,与header指定为列使用哪一行,并index_col为指标来使用的柱:

In [16]: df = pd.read_html(wiki,header=0,index_col=0)[1] 

In [17]: df.head() 
Out[17]: 
     State or union territory Administrative capitals Legislative capitals \ 
No.                    
1 Andaman and Nicobar Islands    Port Blair   Port Blair 
2     Andhra Pradesh   Hyderabad[a]   Hyderabad 
3    Arunachal Pradesh    Itanagar    Itanagar 
4       Assam     Dispur    Guwahati 
5       Bihar     Patna    Patna 

    Judiciary capitals Year capital was established  The Former capital 
No.                   
1    Kolkata       1955  Calcutta (1945–1956) 
2   Hyderabad       1959  Kurnool (1953-1956) 
3    Guwahati       1986      NaN 
4    Guwahati       1975 Shillong[b] (1874–1972) 
5    Patna       1912      NaN 
+0

任何想法,为什么DF的名单时做返回pd.read_table? – MYGz