列标题的set_index等效项

时间:2013-10-01 02:56:38

标签: python pandas

在Pandas中,如果我有一个看起来像这样的DataFrame:

            0       1       2       3       4       5       6
0                2013    2012    2011    2010    2009    2008
1     January   3,925   3,463   3,289   3,184   3,488   4,568
2    February   3,632   2,983   2,902   3,053   3,347   4,527
3       March   3,909   3,166   3,217   3,175   3,636   4,594
4       April   3,903   3,258   3,146   3,023   3,709   4,574
5         May   4,075   3,234   3,266   3,033   3,603   4,511
6        June   4,038   3,272   3,316   2,909   3,057   4,081
7        July           3,661   3,359   3,062   3,354   4,215
8      August           3,942   3,417   3,077   3,395   4,139
9   September           3,703   3,169   3,095   3,100   3,752
10    October           3,727   3,469   3,179   3,375   3,874
11   November           3,722   3,145   3,159   3,213   3,567
12   December           3,866   3,251   3,199   3,324   3,362
13      Total  23,482  41,997  38,946  37,148  40,601  49,764

我可以使用以下方法将第一列转换为索引:

In [55]: df.set_index([0])
Out[55]: 
                1       2       3       4       5       6
0                                                        
             2013    2012    2011    2010    2009    2008
January     3,925   3,463   3,289   3,184   3,488   4,568
February    3,632   2,983   2,902   3,053   3,347   4,527
March       3,909   3,166   3,217   3,175   3,636   4,594
April       3,903   3,258   3,146   3,023   3,709   4,574
May         4,075   3,234   3,266   3,033   3,603   4,511
June        4,038   3,272   3,316   2,909   3,057   4,081
July                3,661   3,359   3,062   3,354   4,215
August              3,942   3,417   3,077   3,395   4,139
September           3,703   3,169   3,095   3,100   3,752
October             3,727   3,469   3,179   3,375   3,874
November            3,722   3,145   3,159   3,213   3,567
December            3,866   3,251   3,199   3,324   3,362
Total      23,482  41,997  38,946  37,148  40,601  49,764

我的问题是如何将第一行转换为列标题? 我能得到的最接近的是:

In [53]: df.set_index([0]).rename(columns=df.loc[0])
Out[53]: 
             2013    2012    2011    2010    2009    2008
0                                                        
             2013    2012    2011    2010    2009    2008
January     3,925   3,463   3,289   3,184   3,488   4,568
February    3,632   2,983   2,902   3,053   3,347   4,527
March       3,909   3,166   3,217   3,175   3,636   4,594
April       3,903   3,258   3,146   3,023   3,709   4,574
May         4,075   3,234   3,266   3,033   3,603   4,511
June        4,038   3,272   3,316   2,909   3,057   4,081
July                3,661   3,359   3,062   3,354   4,215
August              3,942   3,417   3,077   3,395   4,139
September           3,703   3,169   3,095   3,100   3,752
October             3,727   3,469   3,179   3,375   3,874
November            3,722   3,145   3,159   3,213   3,567
December            3,866   3,251   3,199   3,324   3,362
Total      23,482  41,997  38,946  37,148  40,601  49,764

然后我必须进去并删除第一行。

2 个答案:

答案 0 :(得分:3)

处理此问题的最佳方法是避免陷​​入这种情况。

df是如何创建的?例如,如果您使用read_csv或变体,那么header=0会告诉read_csv将第一行解析为列名。


鉴于你拥有df,我认为没有比你描述的更简单的方法来修复它。要删除第一行,您可以使用df.iloc

df = df.iloc[1:]

答案 1 :(得分:1)

我不确定这是否更有效,但您可以尝试使用核心索引和默认列名创建一个数据框,然后使用promlematic数据框重命名列。例如:

import pandas as pd
import numpy as np
from pandas import DataFrame

data = {'0':[' ', 'Jan', 'Feb', 'Mar', 'April'], \
        '1' : ['2013', 3926, 3456, 3245, 1254],  \
        '2' : ['2012', 3346, 4342, 1214, 4522],  \
        '3' : ['2011', 3946, 4323, 1214, 8922]}

DF = DataFrame(data)
DF2 = (DataFrame(DF.ix[1:, 1:]).set_index(DF.ix[1:,0]))
DF2.columns = DF.ix[0, 1:]
DF2