I have a list of lists:
[['1', 'Toy Story (1995)', "Animation|Children's|Comedy"],
['2', 'Jumanji (1995)', "Adventure|Children's|Fantasy"],
['3', 'Grumpier Old Men (1995)', 'Comedy|Romance']]
I want to end up with a pandas dataframe with these columns.
cols = ['MovieID', 'Name', 'Year', 'Adventure', 'Children', 'Comedy', 'Fantasy', 'Romance']
For the 'Adventure', 'Children', 'Comedy', 'Fantasy', 'Romance'
columns, the data will be 1 or 0.
I've tried:
for row in movies_list:
for element in row:
if '|' in element:
element = element.split('|')
However nothing happens to the original list.. Completely stumped here.
Use DataFrame
constructor with str.get_dummies
:
L = [['1', 'Toy Story (1995)', "Animation|Children's|Comedy"],
['2', 'Jumanji (1995)', "Adventure|Children's|Fantasy"],
['3', 'Grumpier Old Men (1995)', 'Comedy|Romance']]
df = pd.DataFrame(L, columns=['MovieID','Name','Data'])
df1 = df['Data'].str.get_dummies()
print (df1)
Adventure Animation Children's Comedy Fantasy Romance
0 0 1 1 1 0 0
1 1 0 1 0 1 0
2 0 0 0 1 0 1
For columns Name
and Year
need split
and rstrip
for remove trailing )
, also Year
is converted to int
s.
df[['Name','Year']] = df['Name'].str.split('\s\(', expand=True)
df['Year'] = df['Year'].str.rstrip(')').astype(int)
Last remove column Data
and add df1
to original by join
:
df = df.drop('Data', axis=1).join(df1)
print (df)
MovieID Name Year Adventure Animation Children's Comedy \
0 1 Toy Story 1995 0 1 1 1
1 2 Jumanji 1995 1 0 1 0
2 3 Grumpier Old Men 1995 0 0 0 1
Fantasy Romance
0 0 0
1 1 0
2 0 1
Here is my version, not good enough for one line answer, but hope it helps you!
import pandas as pd
import numpy as np
data = [['1', 'Toy Story (1995)', "Animation|Children's|Comedy"],
['2', 'Jumanji (1995)', "Adventure|Children's|Fantasy"],
['3', 'Grumpier Old Men (1995)', 'Comedy|Romance']]
cols = ['MovieID', 'Name', 'Year', 'Adventure', 'Children', 'Comedy', 'Fantasy', 'Romance']
final = []
for x in data:
output = []
output.append(x[0])
output.append(x[1].split("(")[0].lstrip().rstrip())
output.append(x[1].split("(")[1][:4])
for h in ['Adventure', 'Children', 'Comedy', 'Fantasy', 'Romance']:
output.append(h in x[2])
final.append(output)
df = pd.DataFrame(final, columns=cols)
print(df)
OUTPUT:
MovieID Name Year Adventure Children Comedy Fantasy \
0 1 Toy Story 1995 False True True False
1 2 Jumanji 1995 True True False True
2 3 Grumpier Old Men 1995 False False True False
Romance
0 False
1 False
2 True
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.