Cecilia
Cecilia

Reputation: 309

How to convert a list to dataframe with multiple columns?

I have a list 'result1' like this:

[[("tt0241527-Harry Potter and the Philosopher's Stone", 1.0),
  ('tt0330373-Harry Potter and the Goblet of Fire', 0.9699),
  ('tt1843230-Once Upon a Time', 0.9384),
  ('tt0485601-The Secret of Kells', 0.9347)]]

I want to convert it into three column dataframe, I tried:

pd.DataFrame(result1)

but this is not what I want

enter image description here

Expected result:

    Number             Title                          Value
 tt0241527  Harry Potter and the Philosopher's Stone   1.0
 tt0330373  Harry Potter and the Goblet of Fire       0.9699

Upvotes: 1

Views: 1815

Answers (3)

Alexandre B.
Alexandre B.

Reputation: 5500

You can try to redifine your input:

[elt1.split('-') + [elt2] for elt1, elt2 in result1[0] ]

Full example:

result1 = [[("tt0241527-Harry Potter and the Philosopher's Stone", 1.0),
  ('tt0330373-Harry Potter and the Goblet of Fire', 0.9699),
  ('tt1843230-Once Upon a Time', 0.9384),
  ('tt0485601-The Secret of Kells', 0.9347)]]
# Columns name in dataframe
columns_name = ["Number", "Title", "Value"]

data = [elt1.split('-') + [elt2] for elt1, elt2 in result1[0] ]
print(data)
# [['tt0241527', "Harry Potter and the Philosopher's Stone", 1.0], 
#  ['tt0330373', 'Harry Potter and the Goblet of Fire', 0.9699],
#  ['tt1843230', 'Once Upon a Time', 0.9384],
#  ['tt0485601', 'The Secret of Kells', 0.9347]]

df = pd.DataFrame(data, columns=columns_name)
print(df)
#       Number                                     Title   Value 
# 0  tt0241527  Harry Potter and the Philosopher's Stone  1.0000
# 1  tt0330373       Harry Potter and the Goblet of Fire  0.9699
# 2  tt1843230                          Once Upon a Time  0.9384
# 3  tt0485601                       The Secret of Kells  0.9347

Upvotes: 4

Celius Stingher
Celius Stingher

Reputation: 18377

This can be used so you can add as many variables as you want in your list without having any issues in the code:

import pandas as pd
    result1 = [[("tt0241527-Harry Potter and the Philosopher's Stone", 1.0),
      ('tt0330373-Harry Potter and the Goblet of Fire', 0.9699),
      ('tt1843230-Once Upon a Time', 0.9384),
      ('tt0485601-The Secret of Kells', 0.9347)]]
    d = []
    for i in range(0,len(result1[0])):
        c = result1[0][i][0].split('-')
        c.append(restul1[0][i][1])
        d.append(c)
    df = pd.DataFrame(d)
    print(df.head())

Output:

           0                                         1       2
0  tt0241527  Harry Potter and the Philosopher's Stone  1.0000
1  tt0330373       Harry Potter and the Goblet of Fire  0.9699
2  tt1843230                          Once Upon a Time  0.9384
3  tt0485601                       The Secret of Kells  0.9347

Finally to rename the columns add:

df.columns = ['Number','Title','Value']
print(df.head())

And you get:

      Number                                     Title   Value
0  tt0241527  Harry Potter and the Philosopher's Stone  1.0000
1  tt0330373       Harry Potter and the Goblet of Fire  0.9699
2  tt1843230                          Once Upon a Time  0.9384
3  tt0485601                       The Secret of Kells  0.9347

Upvotes: 1

Scott Boston
Scott Boston

Reputation: 153510

Try:

results = [[("tt0241527-Harry Potter and the Philosopher's Stone", 1.0),
  ('tt0330373-Harry Potter and the Goblet of Fire', 0.9699),
  ('tt1843230-Once Upon a Time', 0.9384),
  ('tt0485601-The Secret of Kells', 0.9347)]]

df = pd.DataFrame().from_records(results[0])

df[[3,4]] = df[0].str.split('-', expand=True)

print(df)

output:

                                                   0       1          3                                         4
0  tt0241527-Harry Potter and the Philosopher's S...  1.0000  tt0241527  Harry Potter and the Philosopher's Stone
1      tt0330373-Harry Potter and the Goblet of Fire  0.9699  tt0330373       Harry Potter and the Goblet of Fire
2                         tt1843230-Once Upon a Time  0.9384  tt1843230                          Once Upon a Time
3                      tt0485601-The Secret of Kells  0.9347  tt0485601                       The Secret of Kells

Upvotes: 3

Related Questions