删除pandas行中的一部分/增加行的一部分? alignment列标题

所以我有一个数据框,我想要的标题目前不排队:

In [1]: df = pd.read_excel('example.xlsx') print (df.head(10)) Out [1]: Portfolio Asset Country Quantity Unique Identifier Number of fund B24 B65 B35 B44 456 2 General Type A UNITED KINGDOM 1 123 3 General Type B US 2 789 2 General Type C UNITED KINGDOM 4 4852 4 General Type C UNITED KINGDOM 4 654 1 General Type A FRANCE 3 987 5 General Type B UNITED KINGDOM 2 321 1 General Type B GERMANY 1 951 3 General Type A UNITED KINGDOM 2 357 4 General Type C UNITED KINGDOM 3 

我们可以看到 在前2列标题之上有2个空白单元格,下面4列标题下面是我不关心的“B”号码。

所以有两个问题 如何在没有列标题的情况下向上移动前两列(由于上面的空白单元格)?

我怎样才能删除其余列的第2行,并有下面的数据移动代替“B”号码?

我发现了一些类似的问题已经问了python:pandas dataframe中的shift列,但是没有解决上面那些错综复杂的问题,我不这么认为。

另外我对Python和Pandas很新,所以如果这真的是基本的话,我表示歉意!

IIUC你可以使用:

 #create df from multiindex in columns df1 = pd.DataFrame([x for x in df.columns.values]) print df1 0 1 0 Unique Identifier 1 Number of fund 2 Portfolio B24 3 Asset B65 4 Country B35 5 Quantity B44 #if len of string < 4, give value from column 0 to column 1 df1.loc[df1.iloc[:,1].str.len() < 4, 1] = df1.iloc[:,0] print df1 0 1 0 Unique Identifier 1 Number of fund 2 Portfolio Portfolio 3 Asset Asset 4 Country Country 5 Quantity Quantity #set columns by first columns of df1 df.columns = df1.iloc[:,1] 
 print df 0 Unique Identifier Number of fund Portfolio Asset Country \ 0 456 2 General Type A UNITED KINGDOM 1 123 3 General Type B US 2 789 2 General Type C UNITED KINGDOM 3 4852 4 General Type C UNITED KINGDOM 4 654 1 General Type A FRANCE 5 987 5 General Type B UNITED KINGDOM 6 321 1 General Type B GERMANY 7 951 3 General Type A UNITED KINGDOM 8 357 4 General Type C UNITED KINGDOM 0 Quantity 0 1 1 2 2 4 3 4 4 3 5 2 6 1 7 2 8 3 

编辑点评:

 print df.columns Index([u'Portfolio', u'Asset', u'Country', u'Quantity'], dtype='object') #set first row by columns names df.iloc[0,:] = df.columns #reset_index df = df.reset_index() #set columns from first row df.columns = df.iloc[0,:] df.columns.name= None #remove first row print df.iloc[1:,:] Unique Identifier Number of fund Portfolio Asset Country Quantity 1 456 2 General Type A UNITED KINGDOM 1 2 123 3 General Type B US 2 3 789 2 General Type C UNITED KINGDOM 4 4 4852 4 General Type C UNITED KINGDOM 4 5 654 1 General Type A FRANCE 3 6 987 5 General Type B UNITED KINGDOM 2 7 321 1 General Type B GERMANY 1 8 951 3 General Type A UNITED KINGDOM 2 9 357 4 General Type C UNITED KINGDOM 3