Python Forum
Reshaping a single column in to multiple column using Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reshaping a single column in to multiple column using Python
#1
I have an excel file containing a single column (Row's number is not fixed). Using Python 3, I want to,
  1. Import my excel file/data in python,
  2. Read/select the data column (first column), and
  3. Reshape this column into multiple columns having 10 rows in each column and finally
  4. Writing output to a new excel file.
I am a Matlab user and know how to do this in Matlab using Matlab’s reshape command
Quote:newVar = reshape(myColumn, 10, [])
.

Looking for someone to help me out achieving this in Python 3.
Reply
#2
We will help you, but you need to show effort and let us know where you get stuck. Of course, there is not just one way, so my suggestions are how I would approach the problem. As a start, use Pandas to import into a dataframe. Use the iloc function to select your range in the desired column, and copy that data to the new column.

See Pandas documentation
Reply
#3
Ok, I have tried but could not get the desired result.
import pandas as pd
import numpy as np
df =  pd.read_excel('sample.xlsx')
first_column = pd.DataFrame(df.iloc[:,0])
arr = np.array(first_column)
newArr = arr.reshape(arr, (10, -1))
Code shows the error:
Quote:newArr = arr.reshape(10, -1)
Quote:TypeError: only integer scalar arrays can be converted to a scalar index
Reply
#4
reshape is a function, not a method of nparray. The call is "new_array = numpy.reshape(arr, (10, -1))".
Reply
#5
(Jun-19-2022, 07:20 PM)deanhystad Wrote: reshape is a function, not a method of nparray. The call is "new_array = numpy.reshape(arr, (10, -1))".

Hi, thanks for your explanation. I have tried this but no results.

import pandas as pd
import numpy as np
df =  pd.read_excel('sample.xlsx')
myCol = pd.DataFrame(df.iloc[:,0])
arr = np.array (myCol)
newArr = np.reshape(arr, (10, -1), order='F')
np.savetxt("newMatrix.csv", newArr, delimiter=",")
Reply
#6
I have found a strange thing. If i add one more row in my column, the same code performs as intended.
Image
Please note now I have 31 rows. To me it seems that something has to do with indexing.
Reply
#7
The default in pandas read_excel() function is that the first row is used as a header. If you start with data then add the parameter header=None
Reply
#8
Always helps to read the documentation, especially when trying something new.

https://pandas.pydata.org/pandas-docs/st...excel.html

Quote:headerint, list of int, default 0
Row (0-indexed) to use for the column labels of the parsed DataFrame. If a list of integers is passed those row positions will be combined into a MultiIndex. Use None if there is no header.

https://numpy.org/doc/stable/reference/g...shape.html

Quote:numpy.reshape(a, newshape, order='C')[source]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python sql query single quote in a string mg24 1 151 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  python Multithreading on single file mg24 3 268 Nov-05-2022, 01:33 PM
Last Post: snippsat
  Move column to the right if it starts with a letter mfernandes 0 171 Oct-25-2022, 11:22 AM
Last Post: mfernandes
  Delete multiple comments with a single API call (facebook) Ascalon 1 1,219 Oct-21-2022, 02:12 AM
Last Post: OletaSchmeler
  PowerBI: Using Python Regex to look for values in column MarcusR44 1 242 Oct-14-2022, 01:03 PM
Last Post: ibreeden
  Create multiple/single csv file for each sql records mg24 6 391 Sep-29-2022, 08:06 AM
Last Post: buran
  Calculated DF column from dictionary value plantagenet 2 276 Sep-16-2022, 12:46 AM
Last Post: plantagenet
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 260 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  renaming the 0 column in a dataframe Led_Zeppelin 5 481 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  color column in mail html df bnadir55 0 264 Aug-14-2022, 07:11 AM
Last Post: bnadir55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020