Home > Net >  Generate a specific DataFrame with pandas
Generate a specific DataFrame with pandas

Time:11-06

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

CodePudding user response:

Use:

from  itertools import product

df = pd.DataFrame(product(name,language), columns=['Name','Language'])

CodePudding user response:

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})

CodePudding user response:

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'])
  • Related