如何从excel文件中获取特定行

时间:2017-05-13 19:44:15

标签: python pandas

我正在使用pandas读取具有以下结构的Excel文件:

            A                 B         C       D        E
  ╔════════════════════╦════════════╦═══════╦═══════╦══════════╗
1 ║ Project Name       ║            ║       ║       ║          ║
  ╠════════════════════╬════════════╬═══════╬═══════╬══════════╣
2 ║ Date of generation ║            ║       ║       ║          ║
  ╠════════════════════╬════════════╬═══════╬═══════╬══════════╣
3 ║ ID                 ║ Type       ║ Value ║ Color ║ Capacity ║
  ╠════════════════════╬════════════╬═══════╬═══════╬══════════╣
4 ║ 1                  ║ Car        ║ 1,000 ║ Red   ║ 2        ║
  ╠════════════════════╬════════════╬═══════╬═══════╬══════════╣
5 ║ 2                  ║ Truck      ║ 800   ║ Green ║ 12       ║
  ╠════════════════════╬════════════╬═══════╬═══════╬══════════╣
6 ║ 3                  ║ Helicopter ║ 5,000 ║ White ║ 4        ║
  ╚════════════════════╩════════════╩═══════╩═══════╩══════════╝

如您所见,数据标题位于第3行,信息继续在下面。如果我像这样阅读框架:

df = pandas.read_excel("sample.xls", sheetname=0, skiprows=2, header=0)

我的数据框有正确的数据部分,但我无法读取第一个单元格A1和A2中的信息。

我也尝试了以下内容,以便切片'一旦我获得了前两个值,就会出现一个新的数据帧。我收到一个错误:

df = pandas.read_excel("sample.xls", sheetname=0, skiprows=0, header=None)

project_name = df.iloc[0][0]
project_date = df.iloc[1][0]

new_header = df.iloc[2]         #grab the first row for the header
df = df[3:]                     #take the data less the header row
df.rename(columns = new_header) #set the header row as the df header

#tidyup   
df = df.fillna('')              #convert NaN (blank) cells to empty string
df.applymap(str)                #convert all columns to str
df.columns = df.columns.str.strip()   #strip whitespace before and after headers
df_obj = df.select_dtypes(['object']) #select all object columns (not int/float columns)
df[df_obj.columns] = df_obj.apply(lambda x: x.str.strip()) #strip (trim whitepace) of all object columns

运行以上内容我收到错误:

AttributeError: Can only use .str accessor with string values (i.e. inferred_type is 'string', 'unicode' or 'mixed')

我不清楚为什么作为相同的&tidyups部分,但读取df,就像我上面的第一个代码行一样。

有没有办法可以进入上面的标题'通过两次读取文件而不创建全新数据帧的信息?

1 个答案:

答案 0 :(得分:3)

有问题,你忘记分配回来:

class GameScene: SKScene, SKPhysicsContactDelegate {
    var topRightPathNode  : SKShapeNode!
    var bottomRightPathNode  : SKShapeNode!
    var bottomLeftPathNode  : SKShapeNode!
    var topLeftPathNode  : SKShapeNode!
    override init(size: CGSize) {
        super.init(size: size)
        // TOP RIGHT
        let topRightBezierPath = UIBezierPath(arcCenter: CGPoint(x: 0, y:0), radius: 100, startAngle: CGFloat(Double.pi/2), endAngle: 0, clockwise: false)
        topRightPathNode = SKShapeNode(path: topRightBezierPath.cgPath)
        topRightPathNode.strokeColor = SKColor.white
        topRightPathNode.lineWidth = 18
        topRightPathNode.position = CGPoint(x: frame.midX, y:frame.midY)
        addChild(topRightPathNode)
        // BOTTOM RIGHT
        let bottomRightBezierPath = UIBezierPath(arcCenter: CGPoint(x: 0, y:0), radius: 100, startAngle: 0 , endAngle: 3*CGFloat(Double.pi/2), clockwise: false)
        bottomRightPathNode = SKShapeNode(path: bottomRightBezierPath.cgPath)
        bottomRightPathNode.strokeColor = SKColor.orange
        bottomRightPathNode.lineWidth = 18
        bottomRightPathNode.position = CGPoint(x: frame.midX, y:frame.midY)
        addChild(bottomRightPathNode)
        // BOTTOM LEFT
        let bottomLeftBezierPath = UIBezierPath(arcCenter: CGPoint(x: 0, y:0), radius: 100, startAngle: 3*CGFloat(Double.pi/2), endAngle: 2*CGFloat(Double.pi/2), clockwise: false)
        bottomLeftPathNode = SKShapeNode(path: bottomLeftBezierPath.cgPath)
        bottomLeftPathNode.strokeColor = SKColor.green
        bottomLeftPathNode.lineWidth = 18
        bottomLeftPathNode.position = CGPoint(x: frame.midX, y:frame.midY)
        addChild(bottomLeftPathNode)
        // TOP LEFT
        let topLeftBezierPath = UIBezierPath(arcCenter: CGPoint(x: 0, y:0), radius: 100, startAngle: 2*CGFloat(Double.pi/2), endAngle: CGFloat(Double.pi/2), clockwise: false)
        topLeftPathNode = SKShapeNode(path: topLeftBezierPath.cgPath)
        topLeftPathNode.strokeColor = SKColor.blue
        topLeftPathNode.lineWidth = 18
        topLeftPathNode.position = CGPoint(x: frame.midX, y:frame.midY)
        addChild(topLeftPathNode)
    }
}

更好(更快)的是df = df.applymap(str) 使用astype

applymap

所有在一起:

df = df.astype(str)