1

I have 2 variables

name = ['John','Seen','Sam','Tom','Lisa']

language = ['python','java','C++','javascript']

I want to use pandas to generate a DataFrame in a format similar to the following:

name Language
John python
John java
John C++
John javascript
Seen python
Seen java
Seen C++
Seen javascript
Sam python
Sam java
Sam C++
Sam javascript

Thanks

1
  • Please accept the answer if it's solved your issue. :) Commented Nov 5, 2021 at 14:57

3 Answers 3

1

Use:

from  itertools import product

df = pd.DataFrame(product(name,language), columns=['Name','Language'])
Sign up to request clarification or add additional context in comments.

Comments

0

You can try:

import pandas as pd

Knowing that: nameList = ['John','Seen','Sam','Tom','Lisa'] and languageList = ['python','java','C++','javascript'] you do:

df=pd.DataFrame({'name':nameList,'language':languageList})

1 Comment

Your method can only form one-to-one data
0
import pandas as pd

name = ['John','Seen','Sam','Tom','Lisa']
language = ['python','java','C++','javascript']

prod=[]
for i in name:
    for j in language:
        prod.append((i,j))
df=pd.DataFrame(prod, columns=['name','language'])

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.