Reshape python без numpy

Reshaping string in python

Solution 1: Use and sort the values by column and columns Output: Solution 2: Solution for pandas 0.24+ — sorting is not necessary: Use if first column is not index, then remove from columns names and reshape by , last set new index names and use : Solution for old pandas versions — use ordered , because default sorting in function : Question: I have a dataframe that only contains one row, but multiple columns: I want to put every 5 columns to a new row.

Reshaping string in python

I have a coding question where I need to create function to reshape/regroup a string and separate the next group by newline.

The function reshape will accept 2 parameters, the length of the group and the string to reshape. It’ll disregard space in the string, i.e.

reshape(1,"AB CD") #returns A\nB\nC\nD reshape(6, "AB CD SDSD SDSDSDSDSDSD") #returns ABCDSD\nSDSDSD\nSDSDSD\nSD 

This is the my current code

def reshape(n, str): # remove space str = ''.join(str.split()) lines = [] buff = '' for ind,s in enumerate(str): # keep adding the buffer if ****(buff) < n: buff += s else: # if the buffer length exceeds the max group length, add to the return list lines.append(buff) # reset the buffer buff = s return '\\n'.join(lines) 

In my function, the answer somehow always miss the last group part. Is there something wrong with the logic?

Читайте также:  Java произведение двух чисел

This can be solved more efficiently with iterators. Also, don't call your variables str , it overwrites the built-in str :

from itertools import zip_longest def reshape(n, string): # remove space string = string.replace(' ', '') lines = (''.join(chars) for chars in zip_longest(*([iter(string)] * n), fillvalue='')) return '\n'.join(lines) 
reshape(1,"AB CD") 'A\nB\nC\nD' reshape(6, "AB CD SDSD SDSDSDSDSDSD") 'ABCDSD\nSDSDSD\nSDSDSD\nSD' 

BTW, it's about 4-5x faster than the accepted solution.

The problem is that you did not enter the last buffer item to the list in case it doesn't exceed the max group length.

Try adding this after the loop:

Numpy - How to reshape an array in python?, How can we reshape an array in python, For example original_array = [1,2,3,4] and what I want after reshaping is this, [[1,2,3,4]] I reshaped the array using this code, original = np.asarray Stack Overflow

How does Reshape() function works in python?

Can anyone explain what this line is doing in python code ?

X.reshape((X.shape[0], 1) + X.shape[1:])

Basically, this code is changing the shape of X to have an additional (size 1, or singleton if you are used to MATLAB ) dimension. So if the shape was previously (3,3,3) it changes it to (3,1,3,3) . This doesn't add any data since 3x3x3=3x1x3x3=27 It would probably be used so that the number of dimensions match (for functions that include another array). An equivalent form would be:

For more about why you might want to do this, see here

Python | Numpy matrix.reshape(), With the help of Numpy matrix.reshape () method, we are able to reshape the shape of the given matrix. Remember all elements should be covered after reshaping the given matrix. Syntax : matrix.reshape (shape) Return: new reshapped matrix. Example #1 : In the given example we are able to reshape the …

How to reshape data in Python

I have a dataframe that only contains one row, but multiple columns:

I want to put every 5 columns to a new row. Here's the expected output:

The original data was in a list, I converted to a dataframe. I don't know if it's easier to reshape through a list, but here's a sample list for you to try out, the original list is really long. ['review: I stayed around 11 days and enjoyed stay very much.', 'compound: 0.5106, ','neg: 0.0, ','neu: 0.708, ','pos: 0.292, ','review: Plans for weekend stay canceled due to Coronavirus shutdown.','compound: 0.0, ','neg: 0.0, ','neu: 1.0, ','pos: 0.0, ']

It's easier to parse it as a list, then convert it to a dataframe.

  • For each entry, split the entry by ':' and add the key\value to a dictionary
  • Convert the dictionary to a dataframe
import pandas as pd lst = ['review: I stayed around 11 days and enjoyed stay very much.', 'compound: 0.5106, ','neg: 0.0, ','neu: 0.708, ','pos: 0.292, ', 'review: Plans for weekend stay canceled due to Coronavirus shutdown.','compound: 0.0, ','neg: 0.0, ','neu: 1.0, ','pos: 0.0, '] dd = <> for x in lst: sp = x.split(':') if sp[0] in dd: dd[sp[0]].append(sp[1].replace(',',"").****()) else: dd[sp[0]] = [sp[1].replace(',',"").****()] print(dd) print(pd.DataFrame(dd).to_string(index=False)) 
 review compound neg neu pos I stayed around 11 days and enjoyed stay very much. 0.5106 0.0 0.708 0.292 Plans for weekend stay canceled due to Coronavirus shutdown. 0.0 0.0 1.0 0.0 
data_new = ['review: I stayed around 11 days and enjoyed stay very much.', 'compound: 0.5106, ','neg: 0.0, ','neu: 0.708, ','pos: 0.292, ','review: Plans for weekend stay canceled due to Coronavirus shutdown.','compound: 0.0, ','neg: 0.0, ','neu: 1.0, ','pos: 0.0, '] ****_data = ****(data_new) proc_row_mul_of_five = ****_data / 5 j = 5 k = 0 for i in range(0,proc_row_mul_of_five): print(data_new[k:j]) k = i + 5 j = j + 5 

You can try using dictionary

lst = ['review: I stayed around 11 days and enjoyed stay very much.', 'compound: 0.5106, ','neg: 0.0, ','neu: 0.708, ','pos: 0.292, ', 'review: Plans for weekend stay canceled due to Coronavirus shutdown.','compound: 0.0, ','neg: 0.0, ','neu: 1.0, ','pos: 0.0, '] from collections import defaultdict import pandas as pd data_dict = defaultdict(list) for _ in lst: header, value = _.split(':') data_dict [header].append(value.****()) pd.DataFrame.from_dict(data_dict) 

The output is

You can do this easily using numpy

import numpy as np import pandas as pd lis = np.array(['review: I stayed around 11 days and enjoyed stay very much.', 'compound: 0.5106, ','neg: 0.0, ','neu: 0.708, ','pos: 0.292, ','review: Plans for weekend stay canceled due to Coronavirus shutdown.','compound: 0.0, ','neg: 0.0, ','neu: 1.0, ','pos: 0.0, ']) columns = 5 t = np.char.split(lis,":") cols,vals = list(zip(*t)) dff = pd.DataFrame(np.split(np.array(vals),****(vals)/columns), columns=cols[:columns]).replace(",","",regex=True) 

Python - What does -1 mean in numpy reshape?, Reshape your data using array.reshape (-1, 1) if your data has a single feature New shape as (-1, 2). row unknown, column 2. we get result new shape as (6, 2) z.reshape (-1, 2) array ( [ [ 1, 2], [ 3, 4], [ 5, 6], [ 7, 8], [ 9, 10], [11, 12]]) Now trying to keep column as unknown.

Reshape data in python

The dataset looks like this:-

Source Jan_values Feb_values Mar_values ABC 100 200 300 XYZ 200 300 400 

i want to reshape the dataset which should look like this:

Source Month values ABC Jan 100 ABC Feb 200 ABC Mar 300 XYZ Jan 200 XYZ Feb 300 XYZ Mar 400 df = df.stack() 

Use df.melt and sort the values by column source and Month columns

df = pd.DataFrame() df.columns = [c.replace("_values","") for c in df.columns] df = df.melt(id_vars=['source'], var_name='Month') # to sort by month namea months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] df['Month'] = pd.Categorical(df['Month'], categories=months, ordered=True) print(df.sort_values(by=['source','Month'])) 
 source Month value 2 ABC Jan 100 0 ABC Feb 200 4 ABC Mar 300 3 XYZ Jan 200 1 XYZ Feb 300 5 XYZ Mar 400 

Solution for pandas 0.24+ - sorting is not necessary:

Use DataFrame.set_index if first column Source is not index, then remove _values from columns names and reshape by DataFrame.stack , last set new index names and use Series.reset_index :

print (df) Source Jan_values Feb_values Mar_values Apr_values 0 ABC 100 200 300 **** 1 XYZ 200 300 400 467 df1 = (df.set_index('Source') .rename(columns=lambda ****.replace('_values','')) .stack() .rename_axis(['Source','Month']) .reset_index(name='values')) print (df1) Source Month values 0 ABC Jan 100 1 ABC Feb 200 2 ABC Mar 300 3 ABC Apr **** 4 XYZ Jan 200 5 XYZ Feb 300 6 XYZ Mar 400 7 XYZ Apr 467 

Solution for old pandas versions - use ordered CategoricalIndex , because default sorting in function stack :

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] df = df.set_index('Source').rename(columns=lambda ****.replace('_values','')) df.columns = pd.CategoricalIndex(df.columns, categories=months, ordered=True) df1 = df.stack().rename_axis(['Source','Month']).reset_index(name='values') print (df1) Source Month values 0 ABC Jan 100 1 ABC Feb 200 2 ABC Mar 300 3 ABC Apr **** 4 XYZ Jan 200 5 XYZ Feb 300 6 XYZ Mar 400 7 XYZ Apr 467 

Python - NumPy reshape array in-place, 1 Answer. The in-place resize you get with ndarray.resize does not allow for negative dimensions. You can easily check yourself: a=np.array ( [ [0,1], [2,3]]) a.resize ( (4,-1)) > ValueError: negative dimensions not allowed. In most of the cases, np.reshape will be returning a view of the array, and hence there will be no …

Источник

Функция numpy.reshape() в Python — изменение формы массива

Numpy reshape() может создавать многомерные массивы и получать другую математическую статистику. Numpy можно импортировать как import numpy as np. Метод np.reshape() придает новую форму массиву без изменения его элементов.

Что такое функция np.reshape() в Python?

numpy.reshape(array, shape, order = ‘C’) в Python — это математическая функция, которая изменяет форму массиву без изменения его данных. Функция np.reshape() принимает три аргумента и возвращает измененный массив. Для работы с функцией np.reshape() вам необходимо установить numpy для этого руководства.

Давайте посмотрим на синтаксис метода numpy reshape().

Синтаксис

Параметры

  1. array: отображает input_array, форма которого должна быть изменена.
  2. shape: представляет значение int или кортежи int.
  3. order: этот параметр представляет порядок операций. Это может быть либо C_contiguous, либо F_contiguous, где C-порядок управляет ростом строки в массиве, а F-порядок управляет операциями по столбцам.

Возвращаемое значение

Функция Numpy reshape() возвращает массив с новой формой, содержимое которого не изменилось.

Примеры с методом np.reshape()

Пример 1

Программа для демонстрации работы функции reshape().

В приведенном выше коде исходный массив был одномерным массивом.

Итак, в 1-й функции массив был изменен на 3 строки и 2 столбца. Во второй функции массив был изменен на 2 строки и 3 столбца.

Примечание.

В приведенной выше функции массив изменяется только тогда, когда размер массива равен произведению строк и столбцов. Если нет, то оболочка выдаст ошибку.

Пример 2

Как использовать метод ndarray.reshape() в Python

В качестве примера возьмем следующий одномерный массив NumPy ndarray.

Укажите преобразованную форму в виде списка или кортежа в первом аргументе метода reshape() для numpy.ndarray.

Сначала мы определили массив, а затем распечатали форму и размер массива.

Затем мы преобразовали массив в форму [3, 4], а затем распечатали размер и форму массива. Если форма не соответствует количеству элементов в исходном массиве, будет выброшено ValueError.

Преобразование массива из 1D в 3D в Python

Сначала мы воспользуемся функцией np arange() для создания одномерного массива с элементами .9, а затем воспользуемся методом reshape() для преобразования массива в массив(3 x 3).

Вы можете видеть, что мы создали одномерный массив с помощью метода arange(), а затем преобразовали этот массив в трехмерный массив с помощью метода reshape().

Можем ли мы преобразовать массив в любую форму?

Да, можем, если элементы, необходимые для изменения формы, одинаковы в обеих формах.

Мы можем преобразовать 8-элементный 1D-массив в 4 элемента в 2-строчном 2D-массиве, но мы не можем преобразовать его в 3-элементный 3-строчный 2D-массив, для которого потребовалось бы 3×3 = 9 элементов.

Давайте попробуем преобразовать одномерный массив с 8 элементами в двумерный массив с тремя элементами в каждом измерении.

Источник

Русские Блоги

Изменение формы основано на том факте, что элементы массива не могут быть изменены, а количество элементов, содержащихся в новой форме, должно соответствовать исходному количеству элементов. Если элемент массива изменится, будет сообщено об ошибке:

Новый массив, сгенерированный функцией reshape, использует ту же память, что и исходный массив, то есть независимо от того, изменяются ли элементы нового массива или исходного массива, другой массив также изменится соответственно:

Как метод numpy.reshape () переопределяет форму данных?

Сначала сгенерируйте случайный массив

Измените форму на 5 строк и 3 столбца, вы увидите, что оставшиеся два столбца данных в первой строке в (5, 3) используются как первые два столбца второй строки и т. Д.

Измените форму в столбец, где (-1, 1) также может быть любым столбцом, конечно, это может быть только 1, 3 и 5. Обратите внимание, что реализация заключается в циклическом цикле по строкам, от первого столбца до последнего столбца.

Наконец, изменив форму (-1,3), вы увидите, что реализация выполняется по порядку, сколько столбцов данных берется каждый раз, а затем упорядочивается по порядку.

Для многомерных массивов то же

получить,

Источник

Оцените статью