-2

I have a list as follows:

data_content=['Country', 'Capital', 'Currency', 'US', 'Washington', 'USD', 'India',  'Delhi', 'Rupee']

I want to have it as follows:

Country Capital Currency
------------------------

US  Washington  USD

India   Delhi   Rupee

My aim is to then export this table to panda dataframe.

cs95
  • 379,657
  • 97
  • 704
  • 746
gameroller
  • 11
  • 1
  • 2
  • 1
    Which code have you tried? What was wrong? Any error message? – Alexandre Fenyo Sep 17 '17 at 22:33
  • basically i am able to retrieve this list from a pdf, i have a pdf table and want to convert same to panda, but since i use canopy, i am unable to install tabula module. thats what i tried earlier. i am new to the community, please help. thanks. – gameroller Sep 18 '17 at 01:42
  • More importantly than what @cᴏʟᴅsᴘᴇᴇᴅ wrote, though: this type of gimme-teh-codez question showing no attempt or effort (or even question) is discouraged on Stack Overflow. And answers to them are also discouraged. Please show your attempt and we will help with that. – Jean-François Corbett Jan 11 '18 at 09:14

1 Answers1

1

You can proceed as follows:

  1. Split the initial list into groups of 3 elements,
  2. Take the first 3 elements as columns and the rest as data
  3. Use the pd.DataFrame construct to create a pandas.DataFrame:

Here's how the code looks like:

import pandas as pd
data_content=['Country', 'Capital', 'Currency', 'US', 'Washington', 'USD', 'India',  'Delhi', 'Rupee']
data = list(zip(*[iter(data_content)]*3))
pd.DataFrame(data[1:], columns=data[0])

Out:

  Country     Capital Currency
0      US  Washington      USD
1   India       Delhi    Rupee
Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117