在pandas中删除部分行/向上移动部分行?对齐列标题

时间:2016-03-01 10:06:26

标签: python excel pandas

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

    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个问题;如何在没有列标题的情况下向上移动前两列(由于上面的空白单元格)?

如何删除其余列中的第2行并将下面的数据移到上面取代“B”数字?

我发现了一些类似的问题python: shift column in pandas dataframe up by one,但没有解决上述特定错综复杂的问题我不认为。

此外,我对Python和Pandas都很陌生,所以如果这是非常基本的我道歉!

1 个答案:

答案 0 :(得分:1)

您可以使用的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