0

I have got a DataFrame with one of the series containing tuples like:

[('Available', 'Now'),
  ('Age', '27'),
  ('Language', 'English'),
  ('Nationality', 'British'),
  ('Gender', 'Male')]

I need to convert this into a table with first part of the tuple as the column name and second as value.

Thank you!

busybear
  • 10,194
  • 1
  • 25
  • 42
Dmitriy Grankin
  • 568
  • 9
  • 21
  • Possible duplicate of [How do I unnest (explode) a column in a pandas DataFrame?](https://stackoverflow.com/questions/53218931/how-do-i-unnest-explode-a-column-in-a-pandas-dataframe) – yatu Feb 05 '19 at 20:14
  • Are you going to have multiple tuples with "Available", "Age", etc ? – Dani Mesejo Feb 05 '19 at 20:14
  • Example above represents data for one row, I have got multiple rows like this. Also, it if worth mentioning that columns are not entirely consistent, so that is can be some other data present, like ('Smoking', 'No') – Dmitriy Grankin Feb 06 '19 at 08:27

2 Answers2

0

Just convert your list to a dictionary and make a dataframe. You'll need to provide an index since you only have one data point:

pd.DataFrame(dict([
    ('Available', 'Now'),
    ('Age', '27'),
    ('Language', 'English'),
    ('Nationality', 'British'),
    ('Gender', 'Male')
]), index=[0])
busybear
  • 10,194
  • 1
  • 25
  • 42
  • I am tryping to make like this, but it wants to know the columns at the beginning. I do not know my columns though: df=pd.DataFrame() for i in range (len(wanted_merged)): df.loc[len(df)]=pd.DataFrame(dict(features_list[i]), index=[0]) – Dmitriy Grankin Feb 06 '19 at 08:34
  • @DmitriyGrankin Are you able to make the dataframe as you wanted in your post? Are you describing a second problem now? I'm not clear on what you are trying to describe. – busybear Feb 06 '19 at 15:13
0

Just import as list and do transpose

import pandas as pd
df=pd.DataFrame([('Available', 'Now'),
  ('Age', '27'),
  ('Language', 'English'),
  ('Nationality', 'British'),
  ('Gender', 'Male')]).transpose()
df.rename(columns=df.iloc[0]).drop(df.index[0])

Output

Available   Age Language    Nationality Gender
Now         27  English     British     Male
mad_
  • 8,121
  • 2
  • 25
  • 40