Python Forum
Converting list to variables - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Converting list to variables (/thread-29763.html)



Converting list to variables - Palves - Sep-18-2020

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.


RE: Converting list to variables - stullis - Sep-18-2020

They want a pivot table. pandas.Dataframe has a pivot() method.