【问题标题】:delete part of a row in pandas / shift up part of a row ? Align Column Headings删除熊猫行的一部分/上移一行的一部分?对齐列标题
【发布时间】:2016-03-01 10:06:25
【问题描述】:

所以我有一个数据框,其中我想要的标题当前没有排列:

    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 列向上移动(由于上面的空白单元格)?

我怎样才能只删除剩余列的第 2 行,并让下面的数据向上移动以取代“B”数字?

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

另外,我对 Python 和 Pandas 还很陌生,所以如果这真的很基础,我深表歉意!

【问题讨论】:

  • 看起来前 2 个列被读取为索引,调用 df.reset_index() 将它们恢复为列
  • df.columns 是什么?
  • 好吧,在做了df.reset_index() 然后df.columns 他们现在有了标题; “未命名:0”和“未命名:1”这给了我一些东西来称呼那些 2 以将它们向上移动。干杯!

标签: python excel pandas


【解决方案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  

由 cmets 编辑:

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

【讨论】:

  • 这看起来正是我想要做的,但是它抛出了一些错误。首先它给了我IndexError: single positional indexer is out-of-bounds,我已经设法修复它,但现在我得到AttributeError: 'DataFrame' object has no attribute 'str',我不知道如何修复......
  • df1 在您的示例中,第一行之后只有标记为 0 的列;它没有标记为1 的列然后下一行抛出“str”错误
  • print df1.dtypes 是什么?
  • 0 object dtype: object
猜你喜欢
  • 1970-01-01
  • 2020-09-05
  • 2021-12-30
  • 2018-12-24
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多