0

I need to create an Excel spreadsheet dynamically that looks something like this:

 _______________________________________________________
| Name | Start Date | Title | Companies | Sub-companies |
|-------------------------------------------------------|
| Jim  | 01/01/2010 |  CTO  |  Google   |   YouTube     |
|-------------------------------------------------------|
|      |            |       |           |    FitBit     |
|-------------------------------------------------------|
|      |            |       |   Apple   |    NextVR     |
|-------------------------------------------------------|
|      |            |       |           |    Beats      |
|-------------------------------------------------------|
| Tom  | 12/12/2020 |  CEO  |  Amazon   |   Audible     |
|-------------------------------------------------------|
|      |            |       |           |    Zappos     |
|-------------------------------------------------------|
|      |            |       |  Samsung  |  Biologics    |
|-------------------------------------------------------|
|      |            |       |           |    Cheil      |
|-------------------------------------------------------|

This is my code so far:

import pandas as pd

df = pd.DataFrame({
    "Name": ["Jim", "Tom"],
    "Start Date": ["01/01/2010", "12/12/2020"],
    "Companies": ["Google", "Apple", "Amazon", "Samsung"],
    "Sub-companies": [
         "YouTube", "FitBit", "NextVR", "Beats", 
         "Audible", "Zappos", "Biologics", "Cheil"
         ]
})

print(df)

df.to_excel("output.xlsx", sheet_name="Webtoons Comic Data")

This doesn't work because "all arrays must be same length" How can I go about doing this? My current plan is to make it line-by-line by adding empty spaces to each column array. Is there a better way to do it? I don't need help populating the arrays, just structuring the DF itself. Thanks for your help.

4
  • This is more like a multiple index ~ Commented Apr 3, 2021 at 1:44
  • you better input by row rather than by column. Commented Apr 3, 2021 at 1:53
  • @Simon How can I do that? My current idea is to have an array like [None, None, None, "Google", "Youtube"], then add each item to each column Commented Apr 3, 2021 at 1:56
  • @Mick, create an empty dataframe with header first. then insert row by row. Commented Apr 3, 2021 at 1:57

2 Answers 2

1

if you want to insert yourself, good indentation'd be helpful

import pandas as pd
import numpy as np
df = pd.DataFrame(
    columns=[
        'Name', 'Start Date',   'Title', 'Companies',   'Sub-companies',
        ],
    data = np.array([[
        'Jim',  '01/01/2010',   'CTO',  'Google',       'YouTube',
        ], [
        '',     '',             '',     '',             'FitBit',
        ], [
        '',     '',             '',     'Apple',        'NextVR',
    ]]),
    )
Sign up to request clarification or add additional context in comments.

1 Comment

This solution will do the trick, it's not a oneliner like i was hoping but it gives me the flexibility to change it depending on the clients requirements. Thanks.
1

As I mentioned in comments , we can do it by try with set the index

df = pd.DataFrame({'Name':["Jim","Jim","Jim","Jim","Tom","Tom","Tom","Tom"],
"Companies": ["Google","Google", "Apple","Apple", "Amazon", "Amazon","Samsung","Samsung"],
    "Sub-companies": [
         "YouTube", "FitBit", "NextVR", "Beats", 
         "Audible", "Zappos", "Biologics", "Cheil"
         ]
})
df.set_index(['Name','Companies'])
               Sub-companies
Name Companies              
Jim  Google          YouTube
     Google           FitBit
     Apple            NextVR
     Apple             Beats
Tom  Amazon          Audible
     Amazon           Zappos
     Samsung       Biologics
     Samsung           Cheil
#df.set_index(['Name','Companies']).to_excel()

1 Comment

This is helpful, but really the appearance of the data in the spreadsheet is important, is there a way make the company only show once?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.