2

I need to create a dataframe and convert it to CSV so the output will look like this:

People,Age,Pets,Pet Age
Tom,24,Dog,5
Jim,30,Cat,10,
Sally,21,Dog,1
     ,  ,Dog,3
     ,  ,Cat,15
     ,  ,Horse,10

As you can see, there are more pets than people, the relationships between the objects aren't important. The output when changed to Excel should look like:

 _______________________________
| Person | Age | Pets | Pet Age |
|-------------------------------|
|  Tom   | 24  | Dog  |  5      |
|-------------------------------|
|  Jim   | 30  | Cat  |  10     |
|-------------------------------|
|  Sally | 21  | Dog  |  1      |
|-------------------------------|
|        |     | Cat  |  15     |
|-------------------------------|
|        |     | Horse|  10     |
---------------------------------

My code so far is:

df = pd.DataFrame({
    "People": [Tom, Jim, Sally],
    "Age": [24, 30, 21],
    "Pets": [Dog, Cat, Dog, Dog, Cat, Horse],
    "Pet Age": [5, 10, 1, 3, 15, 10]

})

But it's giving me: ValueError: arrays must all be same length

Any help is much appreciated, thanks.

1
  • What exactly is your input? do you have a dict of lists with unequal length, or just some random arrays floating around? Commented Apr 5, 2021 at 3:13

1 Answer 1

5

Instead of the DataFrame() constructor, you can use DataFrame.from_dict() with orient='index':

data = {
    'People': ['Tom', 'Jim', 'Sally'],
    'Age': [24, 30, 21],
    'Pets': ['Dog', 'Cat', 'Dog', 'Dog', 'Cat', 'Horse'],
    'Pet Age': [5, 10, 1, 3, 15, 10],
}

df = pd.DataFrame.from_dict(data, orient='index').T

#   People   Age   Pets  Pet Age
# 0    Tom    24    Dog        5
# 1    Jim    30    Cat       10
# 2  Sally    21    Dog        1
# 3   None  None    Dog        3
# 4   None  None    Cat       15
# 5   None  None  Horse       10

To write as csv:

df.to_csv('pets.csv', index=False)

# People,Age,Pets,Pet Age
# Tom,24,Dog,5
# Jim,30,Cat,10
# Sally,21,Dog,1
# ,,Dog,3
# ,,Cat,15
# ,,Horse,10
Sign up to request clarification or add additional context in comments.

2 Comments

This is good, thanks. But when turn it into a csv or xlsx it adds a column for index, how can i remove that?
You are awesome. Thanks.

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.