1

Basically, I have 2 lists in python

list1 = ['Archaeology', 'Astronomy', 'Biology', 'Botany']
list2 = ['soccer', 'basketball', 'tennis', 'baseball', 'golf', 'running', 'volleyball']

And now, I want to create a DataFrame in Pandas that looks like this :

(Image) How I want the DataFrame to look

How do I go about this task ?

mozway
  • 194,879
  • 13
  • 39
  • 75
Adit Magotra
  • 55
  • 2
  • 7
  • 2
    Why? The organization of pandas implies that there _is_ a relationship for values stored within the same row, but in your case this doesn't seem to be true. Why not use separate Series, perhaps storing them in a container like a dict? – ALollz Aug 24 '21 at 15:00
  • 1
    What have you tried? Have you e. g. tried creating an empty dataframe and assigning those lists as series? – AKX Aug 24 '21 at 15:00
  • Does this answer your question? [Take multiple lists into dataframe](https://stackoverflow.com/questions/30522724/take-multiple-lists-into-dataframe) – DJK Aug 24 '21 at 15:24
  • @DJK it won't work with dictionaries as the list are of different lengths – mozway Aug 24 '21 at 15:25
  • 1
    @mozway, seems more like a minor nuance of the problem, does stackoverflow need every single variant of every single problem? [Heres](https://stackoverflow.com/questions/49891200/generate-a-dataframe-from-list-with-different-length) another identical Q/A – DJK Aug 24 '21 at 15:29
  • @DJK well, sure this is basic, but in my opinion it doesn't take much time to provide the answer and might help a beginner keep enthusiasm ;) – mozway Aug 24 '21 at 15:36

1 Answers1

1

You can simply use the pandas.DataFrame constructor. You can't construct it as columns directly as the number of elements is unequal (this wouldn't work), but you can make it as rows and transpose:

pd.DataFrame([list1, list2], index=['academics', 'sports']).T

output:

     academics      sports
0  Archaeology      soccer
1    Astronomy  basketball
2      Biology      tennis
3       Botany    baseball
4         None        golf
5         None     running
6         None  volleyball
mozway
  • 194,879
  • 13
  • 39
  • 75