Generate table with predefined parameters

Question:

I want to create a table with these parameters:

        # Setting parameters for table
        initial_year=2020
        last_year=2030

        # Setting column names
       
         ## First two columns' names
        The first two columns must have columns with the names 'Wages' and 'Payment'
        
        ### Columns after the second column
        listName = ['column1','column2','column3','column4']

In the end, I need to have the table as the table below

enter image description here

I tried this line of code, but is not working

df=pd.DataFrame(rows=range(initial_year,last_year)).add_prefix('Year')

Can anybody help me how to solve this problem and generate table as table above ?

Asked By: silent_hunter

||

Answers:

You can use:

initial_year=2020
last_year=2030

cols = ['Wages', 'Payment']
listName = ['column1','column2','column3','column4']

df = (pd.DataFrame({'Year': range(initial_year, last_year+1)})
        .reindex(columns=['Year']+cols+listName, fill_value=1)
     )

print(df)

Output:

    Year  Wages  Payment  column1  column2  column3  column4
0   2020      1        1        1        1        1        1
1   2021      1        1        1        1        1        1
2   2022      1        1        1        1        1        1
3   2023      1        1        1        1        1        1
4   2024      1        1        1        1        1        1
5   2025      1        1        1        1        1        1
6   2026      1        1        1        1        1        1
7   2027      1        1        1        1        1        1
8   2028      1        1        1        1        1        1
9   2029      1        1        1        1        1        1
10  2030      1        1        1        1        1        1
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.