1

I am looking online, and I can see how to create columns from a list in Pandas, however I'd like my columns to have repeat values within the rows for each list.

Example is below:

students = ['John','Sally','Mike']
tests = ['Math1','CompSci1','English1']

****Desired Outcome from List*****

'Student'    'Test'
 John         Math1
 John         CompSci1
 John         English1
 Sally        Math1
 Sally        CompSci1
 Sally        English1
 Mike         Math1
 Mike         CompSci1
 Mike         English1

Thanks for any assistance!

SlimJim
  • 169
  • 1
  • 9

1 Answers1

1

One way is itertools.product:

students = ['John','Sally','Mike']
tests = ['Math1','CompSci1','English1']

from itertools import product

df = pd.DataFrame(product(students,tests), columns=['students','tests'])

Output:

  students     tests
0     John     Math1
1     John  CompSci1
2     John  English1
3    Sally     Math1
4    Sally  CompSci1
5    Sally  English1
6     Mike     Math1
7     Mike  CompSci1
8     Mike  English1
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74