import sys import traceback import copy import re import pandas as pd import numpy as np
def decompose_comma(df, comma=','): separated_row_list = [] for i in range(len(df)): tmp = copy.copy(df.iloc[i]) comma_separated_nombre = tmp.nombre.split(comma) for k in range(len(comma_separated_nombre)): tmp.nombre = comma_separated_nombre[k] x = copy.copy(tmp) separated_row_list.append(x) return pd.DataFrame(separated_row_list)
def decompose_hyphen(df, hyphen='-'): separated_row_list = [] for i in range(len(df)): tmp = copy.copy(df.iloc[i]) if not re.match('[0-9]+-[0-9]+', str(tmp.loc['nombre'])): sys.exit if len(tmp.nombre.split(hyphen)) != 2: l = u = int(tmp.nombre) else: l, u = tmp.nombre.split(hyphen) l, u = int(l), int(u) try: if l >= u: raise ValueError() except: traceback.print_exc() sys.exit() for k in range(u - l + 1): tmp.nombre = l + k x = copy.copy(tmp) separated_row_list.append(x) return pd.DataFrame(separated_row_list)
df = pd.read_csv('composed_index.csv') nombre_col = df.columns.get_loc('nombre')
df = decompose_comma(df) df = decompose_hyphen(df)
df.to_csv('decomposed_index.csv', header=True)
|