简体   繁体   English

使用不同大小的列创建 Pandas Dataframe

[英]Create Pandas Dataframe with different sized columns

I need to create a dataframe and convert it to CSV so the output will look like this:我需要创建一个 dataframe 并将其转换为 CSV 所以 output 看起来像这样:

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: output 更改为 Excel 时应如下所示:

 _______________________________
| 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但它给了我: ValueError: arrays must all be the same length

Any help is much appreciated, thanks.非常感谢任何帮助,谢谢。

Instead of the DataFrame() constructor, you can use DataFrame.from_dict() with orient='index' :代替DataFrame()构造函数,您可以使用DataFrame.from_dict()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:写为 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM