Home > Net >  Add year values to all existing rows in dataframe
Add year values to all existing rows in dataframe

Time:12-29

I have a dataframe that looks like this:

df_dict = {'country': ['Japan','Japan','Japan','Japan','Japan','Japan','Japan', 'Greece','Greece','Greece','Greece','Greece','Greece','Greece'],
           'product': ["A", "B", "C", "D", "E", "F", "G", "A", "B", "C", "D", "E", "F", "G"],
           'region': ["Asia","Asia","Asia","Asia","Asia","Asia","Asia","Europe","Europe","Europe","Europe","Europe","Europe","Europe"]}

df = pd.DataFrame(df_dict)

Is there a way to add another column called year with values from 2005 until 2022 for each of these rows? For example, it should look like this:

    country product region year
    Japan   A       Asia   2005
    Japan   A       Asia   2006
    Japan   A       Asia   2007
    ...
    Japan   A       Asia   2022
    Japan   B       Asia   2005
    Japan   B       Asia   2006
    ...

CodePudding user response:

Use merge

res = df.merge(pd.DataFrame(list(range(2005, 2022)), columns=["year"]), how="cross")
print(res)

Output

    country product  region  year
0     Japan       A    Asia  2005
1     Japan       A    Asia  2006
2     Japan       A    Asia  2007
3     Japan       A    Asia  2008
4     Japan       A    Asia  2009
..      ...     ...     ...   ...
233  Greece       G  Europe  2017
234  Greece       G  Europe  2018
235  Greece       G  Europe  2019
236  Greece       G  Europe  2020
237  Greece       G  Europe  2021

[238 rows x 4 columns]
  • Related