Reshape dataframe to dataframe with unlimited rows and filling zeroes where no values


Is there a way to reshape DataFrame to another with unrestricted rows. I just want a DataFrame with 3 columns, no matter how many rows is going to be in DataFrame?

For example,

letters = pd.DataFrame({'Letters' : ['A', 'B', 'C','D', 'E', 'F', 'G', 'H', 'I','J']}) Letters 0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J

and I want to reshape it like this with filling zeroes, where there is no value.

first second third A B C D E F G H I J 0 0

In numpy reshape method as far as I know you need to explicitly identify, how much columns and rows you want..



df = (pd.DataFrame(letters.groupby(letters.index // 3)['Letters'] .apply(list) .values .tolist(), columns=['first','second','third']).fillna(0)) print (df) first second third 0 A B C 1 D E F 2 G H I 3 J 0 0


You could use <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html" rel="nofollow">NumPy reshape</a>. arr.reshape((-1, 3)) tells NumPy to reshape arr to shape (n, 3) where n is computed for you based on the size of arr and the size of the other given dimension(s) (e.g. in this example, the value 3).

import numpy as np import pandas as pd letters = pd.DataFrame({'Letters' : ['A', 'B', 'C','D', 'E', 'F', 'G', 'H', 'I','J']}) arr = np.empty(((len(letters) - 1)//3 + 1)*3, dtype='O') arr[:len(letters)] = letters['Letters'] result = pd.DataFrame(arr.reshape((-1, 3)), columns='first second third'.split()) result = result.fillna(0) print(result)


first second third 0 A B C 1 D E F 2 G H I 3 J 0 0


  • MySQL collation for Portugese
  • Automating a Python Web Crawler - How to prevent raw_input all the time?
  • Populating ArrayList
  • ionic 4 adding side menu
  • NSArrayController without loading a large dataset into an array
  • Using the dot “.” character in MVC4 Routes
  • GetClassificationSpans in Visual Studio 2015 doesn't return anything
  • Join two DataFrames with no common columns for calculations
  • Kendo UI Dropdown, making the drop down panel wider than the control
  • How are Kubernetes CPU limits enforced?
  • How to secure Vaadin flow application with Spring Security
  • max value represented by bigint
  • How is the implicit type conversion priority determined?
  • Facebook oauth/access_token missing
  • Single thread program to make use of multiple cores
  • Why DbSet doesn't implement EnumerableAsync
  • Where are project settings in a Visual Studio website actually stored?
  • How to upload a file using the new controller actions format in sails.js
  • Java generic class, inner class using parameter of outer class
  • BigQuery: straight table format of matrix multiplication into more traditional Matrix multiplication
  • Azure deployment virtual directory [duplicate]
  • Calendar start weeks at Monday
  • Thick border for rounded button in blackberry
  • Errors with Codename One “Send iOS Build” and “Send Android Build”
  • PostgreSQL in Docker - pg_hba.conf to allow access from host to container
  • MVC RC “File” ActionResult, performant?
  • How do I make a Google Sheet script send an email when a specific cell's value changes?
  • Image insertion from SQL info
  • How to set title name of the pdf. While viewing the Document(New Tab)
  • Why does packing not work across sibling unions or structs
  • pass sessionid through jquery ajax call to php
  • handlers inside a component invoking eachothers code by mistake
  • how to get the location(lat/lng) on google maps v3 from the location(x,y)