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.
https://stackoverflow.com/questions/66947749/create-pandas-dataframe-with-different-sized-columns April 05, 2021 at 10:55AM
没有评论:
发表评论