Reputation: 147
I would like to create a dataframe from 2 python lists that I have.
Say I have the following 2 lists;
x = [1,2,3]
z = ['a','v','d']
I have initialized a dataframe
data = pd.DataFrame(columns = ['Type', 'Data']
and this is one of the things that I have tried doing.
df = data.append({'Type': x, 'Data' : z}, ignore_index = True)
but it results in the following dataframe
Type Data
0 [1, 2, 3] [a, v, d]
However, this is what I actually want it to look like
Type Data
0 1 a
1 2 v
2 3 d
How would I do this? Thank you in advance.
Upvotes: 1
Views: 2710
Reputation: 19947
You can use assign:
data.assign(Type=x, Data=z)
Type Data
0 1 a
1 2 v
2 3 d
Upvotes: 0
Reputation: 6920
Try this :
df = pd.DataFrame(zip(x,z),columns=['Type', 'Data'])
print(df)
Output :
Type Data
0 1 a
1 2 v
2 3 d
Upvotes: 2
Reputation: 862406
Convert dictionary to DataFrame
:
data = pd.DataFrame(columns = ['Type', 'Data'])
df = data.append(pd.DataFrame({'Type': x, 'Data' : z}), ignore_index = True)
print (df)
Type Data
0 1 a
1 2 v
2 3 d
Upvotes: 1