Python: convert variables into correct format for DataFrame

I have 3 variables that I would like to use to build my dataset but since they are in a weird shape/format, I had no success so far. I'm quite new to this and really appreciate any help!!

The 3 variables I have are:

print(newspaper)

['Bolero']

['Schweizer Illustrierte Style']

['Bolero']

print(title)

['Schönheit und Tragik']

['magie pur']

['Das sind unsere Favoriten']

print(pubDate)

['2007-01-01']

['2007-01-01']

['2007-01-01']

It seems to like all variables are a list of lists, but I'm not quite sure. However, since the data is scrapped from a private website, I can't post the entire code here, but I hope this is already enough for you to access what the problem is with that variable format.

What I would like to have is a dataset of this format:

Newspaper Title PubDate
Bolero Schönheit und Tragik 2007-01-01
Schweizer Illustrierte Style magie pur 2007-01-01
Bolero. Das sind unsere Favoriten 2007-01-01

Topic dataframe data-formats python

Category Data Science


First, you need to convert list of list into a list.

From link, you can convert a list of lists into a list by declaring the following function.

flatten = lambda t: [item for sublist in t for item in sublist]

Now all you need is to create dataframe using created lists.

data = {"Newspaper":flatten(newspaper), "Title": flatten(title), "PubDate": flatten(pubDate)}
pd.DataFrame.from_dict(data)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.