Home > front end >  Merging tables and concatenate strings in specific table
Merging tables and concatenate strings in specific table

Time:10-17

I am trying to merge two different tables into one table. The first table is Pandas data frame that contains information about the period years from 2000 until 2005 or six observations:

time_horizon=pd.DataFrame(range(2000,2005 1)) 

Now I want to concatenate this text 'WT' with the previous time_horizon

time_horizon str('WT')

After this next step should be to add specific values for this observation

values=pd.DataFrame(range(1,7)) 

In the end, I need to have a data frame as data frame showed on the pic below

enter image description here

The second step for concatenation not works for me so I can't implement the third step and make this table.

So can anybody help me how to make this table?

CodePudding user response:

solution to the second step that failed for you.

str('WT') (time_horizon).astype(str)
0
0   WT2000
1   WT2001
2   WT2002
3   WT2003
4   WT2004
5   WT2005

One way to solve it is

# create a df, with columns only
df=pd.DataFrame(columns=range(2000,2005 1)).add_prefix('WT')

# fill first column with range of values
df.iloc[:,0]= range(1,7)

# forward fill across rows
df.ffill(axis=1)

    WT2000  WT2001  WT2002  WT2003  WT2004  WT2005
0   1   1   1   1   1   1
1   2   2   2   2   2   2
2   3   3   3   3   3   3
3   4   4   4   4   4   4
4   5   5   5   5   5   5
5   6   6   6   6   6   6
  • Related