简体   繁体   中英

Creating a dataframe from multiple lists with list names as column names

I have 3 lists:

name = ['Robert']
age = ['25']
gender = ['m']

I want to create a dataframe like the one shown below(with name of the list as column names):

在此输入图像描述

This is what I'm doing to get this dataframe :

data=pd.DataFrame([name,age,gender]).T
data.columns=['name','age','gender']

I want to know whether there is a better way of doing this

The fastest way:

pd.DataFrame(dict(name=['Robert'],age=['25'],gender=['m']))

pd.DataFrame takes data as first parameter which is: numpy.ndarray , dict , or DataFrame .

Considering that you don't have more variables than name , age , and gender defined, I think this might work:

not_my_data = set(dir())
# define your variables
name=['Robert']
age=['25']
gender=['m'].

my_data = set(dir()) - not_my_data

pd.DataFrame({k:globals()[k] for k in my_data})

Dataframe from columns

Note the pd.DataFrame constructor accepts a dictionary of column labels mapped to lists of values. So you can use:

df = pd.DataFrame({'name': name, 'age': age, 'gender': gender'})

Dataframe from rows

Alternatively, you can feed rows using a list comprehension with zip . This creates a list of lists, each sublist representing a single row:

name = ['Robert']
age = ['25']
gender = ['m']

L = [list(row) for row in zip(name, age, gender)]
df = pd.DataFrame(L, columns=['name', 'age', 'gender'])

print(df)

     name age gender
0  Robert  25      m

The above can be written functionally using map :

L = list(map(list, zip(name, age, gender)))

Option 1

d = {'name':['Robert'],'age':['25'],'gender':['m']}
pd.DataFrame.from_dict(d)

Option 2 Form the dict on the fly -

pd.DataFrame.from_dict(dict(name=['Robert'], age=['25'], gender=['m']))
    name=['Robert']
    age=['25']
    gender=['m']
    data = pd.DataFrame({"name":name,"age":age,"gender":gender})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM