def split_data_set(request): pk = request.data['id'] file = get_object_or_404(File, id=pk) df = dataframe_from_file(file.file) response = {'training_set': {}, 'test_set': {}, 'error': False} try: training_set, test_set = train_test_split(df, test_size=0.2) response = { 'training_set': format_to_json(pd.DataFrame(training_set)), 'test_set': format_to_json(pd.DataFrame(test_set)), 'error': False } except Exception as e: response['error'] = str(e) return Response(response)
def execute_query(request): pk = request.data['id'] file = get_object_or_404(File, id=pk) query_string = request.data['query'] df = dataframe_from_file(file.file) results = sqldf(query_string, locals()) return Response(format_to_json(results))
def filter_by_columns(request): pk = request.data['id'] file = get_object_or_404(File, id=pk) columns = request.data['columns_names'].split(',') columns_list = [] df = dataframe_from_file(file.file) if columns and columns[0]: for column in columns: columns_list.append(column) df = df[columns_list] return Response(format_to_json(df))
def transform(request): pk = request.data['id'] column = request.data['column'] convert_to = request.data['type'] file = get_object_or_404(File, id=pk) df = dataframe_from_file(file.file) try: df = df.astype({column: convert_to}, errors='ignore') except Exception as e: pass return Response(format_to_json(df))
def search_value(request): pk = request.data['id'] file = get_object_or_404(File, id=pk) df = dataframe_from_file(file.file) value = request.data['value'] data = '' if value in df.columns: data = df[[value]] elif value in df.index: data = df.loc[[value]] else: data = df[df.isin([value]).any(1)] return Response(format_to_json(data))
def filter_by_columns_and_rows(request): pk = request.data['id'] file = get_object_or_404(File, id=pk) df = dataframe_from_file(file.file) begin_line = int( request.data['beginLine']) if request.data['beginLine'] else '' end_line = int(request.data['endLine']) if request.data['endLine'] else '' begin_column = int( request.data['beginColumn']) if request.data['beginColumn'] else '' end_column = int( request.data['endColumn']) if request.data['endColumn'] else '' last_line = df.shape[0] + 1 last_column = df.shape[1] + 1 filtered_data = df if (begin_line != '' and (begin_line < 0 or begin_line >= last_line - 1)) or ( end_line != '' and end_line > last_line) or ( begin_column != '' and (begin_column < 0 or begin_column >= last_column - 1)) or ( end_column != '' and end_column > last_column): return HttpResponse( "Indices invalides (Index lignes ou colonnes en dehors de la plage autorisée)" ) if begin_line != '' and end_line != '' and begin_column != '' and end_column != '': filtered_data = df.iloc[begin_line:end_line, begin_column:end_column] elif begin_line == '' and end_line != '' and begin_column != '' and end_column != '': filtered_data = df.iloc[:end_line, begin_column:end_column] elif begin_line == '' and end_line == '' and begin_column != '' and end_column != '': filtered_data = df.iloc[:, begin_column:end_column] elif begin_line == '' and end_line == '' and begin_column == '' and end_column != '': filtered_data = df.iloc[:, :end_column] elif begin_line != '' and end_line == '' and begin_column == '' and end_column == '': filtered_data = df.iloc[begin_line:, :] elif begin_line != '' and end_line != '' and begin_column == '' and end_column == '': filtered_data = df.iloc[begin_line:end_line, :] elif begin_line != '' and end_line != '' and begin_column != '' and end_column == '': filtered_data = df.iloc[begin_line:end_line, begin_column:] elif begin_line != '' and end_line == '' and begin_column == '' and end_column != '': filtered_data = df.iloc[begin_line:, :end_column] elif begin_line != '' and end_line != '' and begin_column == '' and end_column != '': filtered_data = df.iloc[begin_line:end_line, :end_column] elif begin_line != '' and end_line == '' and begin_column != '' and end_column != '': filtered_data = df.iloc[begin_line:, begin_column:end_column] elif begin_line == '' and end_line != '' and begin_column != '' and end_column == '': filtered_data = df.iloc[:end_line, begin_column:] elif begin_line != '' and end_line == '' and begin_column != '' and end_column == '': filtered_data = df.iloc[begin_line:, begin_column:] elif begin_line == '' and end_line != '' and begin_column == '' and end_column != '': filtered_data = df.iloc[:end_line, :end_column] return HttpResponse(format_to_json(filtered_data))
def upload(request): file = File(file=request.data['file']) name, ext = os.path.splitext(file.file.name) file.title = name file.save() df = dataframe_from_file(file.file) json_response = {'data': 'Extension non valide'} if len(df) != 0: json_data = format_to_json(df) columns = df.columns.values.tolist() json_response = { 'name': name, 'data': json_data, 'path': file.file.url, 'columnsNames': columns, 'id': file.id, 'size': df.size, 'rows': df.shape[0], 'columns': df.shape[1], } else: file.delete() return Response(json_response)
def reset(request): pk = request.data['id'] file = get_object_or_404(File, id=pk) df = dataframe_from_file(file.file) return Response(format_to_json(df))
def describe(request): pk = request.data['id'] file = get_object_or_404(File, id=pk) df = dataframe_from_file(file.file) json_object = format_to_json(df.describe().transpose()) return Response(json_object)