Snippets

openpyxl Converting a Pandas DataFrame to a format that openpyxl can work with

Created by CharlieC
# based on code provided by Jeff Reback at PyCon 2015 in Montreal

from pandas import Timestamp
import numpy as np

from pandas.util import testing
df = testing.makeMixedDataFrame()
df.iloc[0] = np.nan


blocks = df._data.blocks
ncols = sum(b.shape[0] for b in blocks)
data = [None] * ncols


for b in blocks:
    # convert blocks into lists with conversion of datetimes
    # expands contiguous blocks

    if b.dtype.type == np.datetime64:
        # cast to timestamp, direct conversion to serial might be preferable
        values = np.array([Timestamp(v) for v in b.values.ravel()])
        values = values.reshape(b.shape)

    else:
        values = b.values

    result = values.tolist()

    for col_loc, col in zip(b.mgr_locs, result):
        data[col_loc] = col


for i in df.index:
    row = [data[j][i] for j in range(ncols)] # could be appended to an openpyxl worksheet

Comments (1)

  1. Paul Symonds

    Can you give an example of how to use this and also is it possible to append to openpyxl sheet at a given cell?

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.