使用Python提取和合并Excel数据

时间:2017-10-01 22:00:51

标签: python excel pandas

我有一个包含40张左右的Excel(.xlsx)文件。每张纸具有相同的结构,但包含不同的数据。我想从每张纸中提取信息并将其合并为一张,每张纸的信息叠加在另一张纸上。我需要从每个中提取的信息有两个:

  1. 工作表名称,始终位于单元格E3中
  2. 感兴趣的细胞区域,始终位于第72-85行和第E-V列
  3. 之间

    此提取的信息将粘贴在合并工作表的第2-15行中,工作表名称在一列中,所有其他信息在其旁边的列中。然后,来自提取的下一张纸的信息将被粘贴到第16-29行等等。

    我是Python的新手,我正在运行Python 3.6.1。我已经找到了如何使用Python组合Excel文件的解决方案,但没有用于从Excel工作表中的指定单元格中提取信息。

    任何指导都会非常有用。

    更新1:我设法将一张纸张的区域加载到数据框中。

    // asynchronous function
    exports.playlistPlayer = async (req, res, next) => {
      // wait for the findById method promise to resolve
      const playlist = await Playlist.findById({
        _id: req.body.playlist._id
      })
    
      // wait for finding all songs in db whose id's are in
      // the playlist.songs array
      const songs = await Song.find({
        _id: { $in: playlist.songs }
      })
    
      // create the customAlbum by using the map method to
      // tramsform the song objects to the required form
      const customAlbum = songs.map(song => ({
        title: song.title,
        time: song.time,
        source: song.source,
        song_id: song._id
      }))
    
      // and there you should now have your customAlbum array
      console.log(customAlbum)
    
      // now you can use it for example
      // to return a response to the client:
      // res.json(customAlbum)
    }
    

    但是,我仍然需要将工作表名称添加到数据框中,然后在所有工作表上运行相同的循环,然后最终将每个工作表中的信息一起附加到一个数据框/文件中。

1 个答案:

答案 0 :(得分:2)

这应该足以让你入门:

my_list

如果您已经非常熟悉excel,那么您将快速拿起大熊猫。你应该绝对考虑使用python的jupyter笔记本。 Jupyter笔记本基本上会创建一个像UI这样的电子表格,其中包含大量功能,用于运行分析和操作数据。

参考文献:

Use PANDAS to read multiple sheets from same workbook

Link to jupyter notebook documentation

Pandas tutorials

Practical business solutions with pandas for python

相关问题