add a distinct element from a date list and a coountrylist to the dataframe

The name of the process you want is the “cartesian product” of 2 lists. You can use python’s built-in library itertools.product to achieve this, and then construct a DataFrame:

import itertools
data = itertools.product(date_list, country_list)
df = pd.DataFrame(data, columns=["date", "country"])

         date  country
0  2020-05-01  Germany
1  2020-05-01  England
2  2020-05-02  Germany
3  2020-05-02  England
4  2020-05-03  Germany
5  2020-05-03  England
6  2020-05-04  Germany
7  2020-05-04  England

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top