From 89f5d5c30a57eb5dcfef7b85a8a12c028424d856 Mon Sep 17 00:00:00 2001 From: fatz Date: Mon, 3 Jun 2019 17:57:55 +0200 Subject: [PATCH] generate a Series dataframe from the dict list to allow for columns with different lengths --- pandas_ods_reader/parser.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pandas_ods_reader/parser.py b/pandas_ods_reader/parser.py index ec9d49f..f40702e 100644 --- a/pandas_ods_reader/parser.py +++ b/pandas_ods_reader/parser.py @@ -21,7 +21,7 @@ def load_ods(doc, sheet, headers=True, columns=None): # row is a list of cells if headers and i == 0 and not columns: # columns as lists in a dictionary - df_dict = {cell.value: pd.Series() for cell in row if cell.value} + df_dict = {cell.value: [] for cell in row if cell.value} # create index for the column headers col_index = { j: cell.value for j, cell in enumerate(row) if cell.value} @@ -30,7 +30,7 @@ def load_ods(doc, sheet, headers=True, columns=None): columns = columns if columns else ( ["column_%s" % j for j in range(len(row))]) # columns as lists in a dictionary - df_dict = {column: pd.Series() for column in columns} + df_dict = {column: [] for column in columns} # create index for the column headers col_index = {j: column for j, column in enumerate(columns)} if headers: @@ -38,11 +38,16 @@ def load_ods(doc, sheet, headers=True, columns=None): for j, cell in enumerate(row): if j < len(col_index): # use header instead of column index - df_dict[col_index[j]].append(pd.Series([cell.value])) + print(cell.value) + df_dict[col_index[j]].append(cell.value) else: continue + # convert lists to pd.Series + df_series = {} + for col in df_dict.keys(): + df_series[col] = pd.Series(df_dict[col]) # and convert to a DataFrame - df = pd.DataFrame(df_dict) + df = pd.DataFrame(df_series) return df