Python Forum

Full Version: Converting list to variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Guys,

I'm really stuck on this one. I was given a file that looks like this.
Output:
id original 0 1 [['U K', 1], ['U S A', 2]] 1 2 [['U K', 2], ['U E A', 1]] 2 3 [] 3 4 [['France', 3]] 4 5 [['U K', 6]]
(the code is just to generate this sample table that matches exactly the format I was given.)
data = {'id':[1, 2, 3, 4, 5], 'original':["[['U K', 1], ['U S A', 2]]", ["['U K', 2], ['U E A', 1]"], [], ["['France', 3]"]
,["['U K', 6]"]]}
df = pd.DataFrame(data)
print (df)
And I am asked to convert the same table to look like this.

Output:
id original U_K U_S_A U_E_A France 0 1 [['U K', 1], ['U S A', 2]] 1 2 1 2 [['U K', 2], ['U E A', 1]] 2 1 2 3 [] 3 4 [['France', 3]] 3 4 5 [['U K', 6]] 6
(again code just to generate the desirable output)
data = {'id':[1, 2, 3, 4, 5], 'original':["[['U K', 1], ['U S A', 2]]", ["['U K', 2], ['U E A', 1]"], [], ["['France', 3]"]
,["['U K', 6]"]], 'U_K':[1,2,"","",6], 'U_S_A':[2,"","","",""],'U_E_A':["",1,"","",""],'France':["","","",3,""]}
df = pd.DataFrame(data)
print (df)
I cannot change the format, It is given. The original file has 200k rows and, after converting, 200 columns... And I am at lost on this one.

Any help will be appreciated, thanks in advance.
They want a pivot table. pandas.Dataframe has a pivot() method.